问题描述
我已经开发了一种行为,该行为更改了AssociatedObject的Clip属性.当我运行该应用程序时,一切都很好.但是,当我在Blend中查看页面时,该行为似乎并不影响其关联对象.
I have developed a behavior that changes the Clip property of the AssociatedObject. When I run the application, all is well. But when I view the page in Blend it appears like the behavior does not affect its associated object.
我试图通过将Visual Studio 2010调试器附加到它的进程来调试"混合,并在行为的OnAttached方法上设置一个断点,但是从未达到断点.好像blend阻止了该行为在设计时那样.
I tried to "debug" blend by attaching visual studio 2010 debugger to it's process and set a break point on the behavior's OnAttached method, but the breakpoint is never reached. As if blend prevents that behavior from being attached at design time.
有办法解决吗?
干杯
推荐答案
我终于找到了一种可行的解决方法,但是在此答案的结尾我有一个很大的警告.这是我的CustomAttachManager:
I finally found a workable way around it, but there's a big caveat which I put at the end of this answer. Here's my CustomAttachManager:
public class CustomAttachManager : DependencyObject
{
#region Object CustomAttachManager.Attached = null
public static IAttachedObject GetAttached(DependencyObject obj) { return (IAttachedObject)obj.GetValue(AttachedProperty); }
public static void SetAttached(DependencyObject obj, IAttachedObject value) { obj.SetValue(AttachedProperty, value); }
public static readonly DependencyProperty AttachedProperty =
DependencyProperty.RegisterAttached("Attached", typeof(IAttachedObject), typeof(CustomAttachManager), new PropertyMetadata(null, StaticHandleAttachedChanged));
static void StaticHandleAttachedChanged(DependencyObject self, DependencyPropertyChangedEventArgs args)
{
var ov = (IAttachedObject)args.OldValue;
var nv = (IAttachedObject)args.NewValue;
if (ov != null) ov.Detach();
if (nv != null) nv.Attach(self);
}
#endregion
}
然后您可以使用它来附加如下行为:
You can then use it to attach behaviors like this:
<my:CustomAttachManager.Attached>
<my:RedBackgroundBehavior></my:RedBackgroundBehavior>
</my:CustomAttachManager.Attached>
我的示例行为将面板的背景更改为红色,这在设计器中是可见的.
My example behavior changes the background of a panel to red, which is visible in the designer.
剩下的问题是多种行为的情况.我能想到的最好的解决方案是代理:
The remaining problem is the case of multiple behaviors. The best solution I could come up with is a proxy:
[ContentProperty("Children")]
public class MultipleBehavior : Behavior<DependencyObject>
{
DependencyObjectCollection<IAttachedObject> children = new DependencyObjectCollection<IAttachedObject>();
public DependencyObjectCollection<IAttachedObject> Children { get { return children; } }
protected override void OnAttached()
{
foreach (var child in Children) child.Attach(AssociatedObject);
}
protected override void OnDetaching()
{
foreach (var child in Children) child.Detach();
}
}
可通过以下方式使用:
<my:CustomAttachManager.Attached>
<my:MultipleBehavior>
<my:RedBackgroundBehavior />
<my:SupriseBehavior />
</my:MultipleBehavior>
</my:CustomAttachManager.Attached>
代理具有一个缺陷,即它无法正确处理在已附加某些东西之后添加的行为,但是在经典用例中不会发生这种情况.
The proxy has a defect in that it won't properly handle behaviors added after something already got attached, but that is not going to happen in the classic use case.
需要注意的是,我不知道使Blend显示对象树中行为的方法.无法使用"AlternateContentProperty",因为它不适用于CustomAttachManager使用的附加属性.
The caveat is that I don't know a way to make Blend display the behaviors in the object tree. The "AlternateContentProperty" can't be used because it doesn't work for attached properties, which is what CustomAttachManager uses.
如果我找到此问题的答案,我将更新此答案.
I will update this answer should I find an answer to this problem.
这篇关于在设计时未附加Silverlight混合行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!