大佬教程收集整理的这篇文章主要介绍了Silverlight混合行为在设计时不会附加,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试通过将visual studio 2010调试器附加到它的进程来“调试”混合,并在行为的OnAttached方法上设置断点,但是从未到达断点.好像混合物可以防止在设计时附加这种行为.
有办法解决吗?
干杯,
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 }
然后,您可以使用它来附加这样的行为:
<my:CustomAttachManager.Attached> <my:redBACkgroundBehavior></my:redBACkgroundBehavior> </my:CustomAttachManager.Attached>
我的示例行为将面板的背景更改为红色,这在设计器中可见.
剩下的问题是多种行为的情况.我能提出的最佳解决方案是代理:
[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>
代理有一个缺陷,即它不能正确处理已经附加的东西后添加的行为,但在经典用例中不会发生这种情况.
需要注意的是,我不知道如何让Blend在对象树中显示行为.无法使用“AlternateContentProperty”,因为它不适用于附加属性,这是CustomAttachManager使用的属性.
如果我找到这个问题的答案,我会更新这个答案.
以上是大佬教程为你收集整理的Silverlight混合行为在设计时不会附加全部内容,希望文章能够帮你解决Silverlight混合行为在设计时不会附加所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。