gpt4 book ai didi

Silverlight 混合行为不会在设计时附加

转载 作者:行者123 更新时间:2023-12-01 04:04:49 25 4
gpt4 key购买 nike

我开发了一种更改 AssociatedObject 的 Clip 属性的行为。当我运行应用程序时,一切都很好。但是当我在 Blend 中查看页面时,它似乎行为不会影响其关联的对象。

我试图通过将 Visual Studio 2010 调试器附加到它的进程并在行为的 OnAttached 方法上设置断点来“调试”混合,但从未达到断点。好像混合可以防止在设计时附加该行为。

有办法解决吗?

干杯,

最佳答案

我终于找到了一种可行的方法来解决它,但是我在这个答案的末尾提出了一个很大的警告。这是我的 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
}

然后,您可以使用它来附加如下行为:
<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 混合行为不会在设计时附加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10154023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com