在| generic.xaml中使用样式设置器设置类型为Control的属性会导致VS崩溃

问题描述

|| 在Generic.xaml中的主控件样式中,我试图设置主控件属性认值(并且此属性的类型为Control),但这会导致VS 2010在设计中使用MyMainControl时崩溃视图。使用下面的代码可以看到该问题。我正在使用Silverlight4。我还尝试将MyControl指定为资源(如下面的代码中注释所示),这也导致VS 2010在使用MyMainControl时崩溃。 我发现,如果在MyControl构造函数删除\“ DefaultStyleKey = typeof(MyControl)\”,则不会发生此问题(但是MyControl \的样式未应用到该控件,并且该控件看起来不正确)。 这里的主要目标是,我需要允许MyMainControl类创建MyControl类的实例,以在MyMainControl类的实例化期间将MyControl属性设置为。认情况下,我希望将MyControl属性设置为MyControl的实例,但是我希望用户能够为其XAML中的MyControl属性指定null(或使用不同设置指定MyControl实例)。他们这样做了,我希望MyControl的认内部实例化不发生(以提高MyMainControl的效率)。 我最初是在MyMainControl的构造函数中创建MyControl实例的,但是如果用户在MyMainControl XAML中将MyControl属性设置为null,则仍然在内部不必要地创建MyControl实例(然后将其覆盖为null),这将导致性能大大降低。 (比起只使用null而不是在内部创建MyControl)。 在WPF中,我可以将MyControl指定为资源,然后在MyMainControl的generic.xaml样式中,我可以将MyControl属性设置为MyControl类的实例,这大大提高了MyMainControl的性能如果用户将MyControl设置为null。但是我无法在Silverlight中获得同样的效果。 总而言之,主要思想是,如果我可以使用MyMainControl样式为MyControl属性指定认值,则用户将能够指定自己的MyMainControl样式,在其中他们可以为MyControl属性指定null值。或具有不同设置的MyControl实例-由于它们的MyMainControl样式将替换我的样式,因此不会发生我在MyMainControl样式中指定的MyControl的认实例化。 我非常感谢任何人都可以提供的有关在Silverlight中如何完成此操作的帮助!
<ResourceDictionary
xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"
xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"
xmlns:local=\"clr-namespace:MyNamespace\"
>

<Style targettype=\"local:MyControl\">
    <Setter Property=\"Opacity\" Value=\"1\" />
</Style>

<!-- Note: I tried specifying my control as a resource
(so I Could in the MyMainControl style set the property to
the MyControl StaticResource),but include this line of XAML
causes VS 2010 to crash when using my main control.
<local:MyControl x:Key=\"MyControl\" />-->

<Style targettype=\"local:MyMainControl\">
    <Setter Property=\"MyControl\" >
        <Setter.Value>
            <local:MyControl />
        </Setter.Value>
    </Setter>
</Style>
MyControl是一个非常简单的类,它从Control派生而来:
public class MyControl : Control
{
    public MyControl()
    {
        DefaultStyleKey = typeof(MyControl);
    }
}
MyMainControl MyControl DependencyProperty的代码是:
public partial class MyMainControl : Control
{
...
    public static readonly DependencyProperty MyControlProperty = DependencyProperty.Register(\"MyControl\",typeof(MyControl),typeof(MyMainControl),new PropertyMetadata(null));

    public MyControl MyControl
    {
        get
        {
            return (MyControl)GetValue(MyControlProperty);
        }
        set
        {
            SetValue(MyControlProperty,value);
        }
    }
}
    

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)