当我提供默认值时,为什么依赖属性实现会使我的应用程序崩溃?

发布于 2024-07-27 19:34:52 字数 1832 浏览 2 评论 0原文

当我提供默认值时,为什么依赖属性实现会使我的应用程序崩溃?

这段代码位于我的 UserControl 对象的类声明中。 一切工作正常 - 它编译和运行完美。

public static System.Windows.DependencyProperty DepProp
    = System.Windows.DependencyProperty.Register(   "Rect",
                                                    typeof(System.Windows.Shapes.Rectangle),
                                                    typeof(FooControl));
public System.Windows.Shapes.Rectangle Rect
{
    get
    { return ((System.Windows.Shapes.Rectangle)(GetValue(DepProp))); }
    set
    { SetValue(DepProp, value); }
}

但是,当我将默认值添加到依赖项属性时:
该代码可以编译,但在尝试实例化 UserControl 时崩溃并出现致命异常。

作为参考,我的代码现在看起来像这样 - 添加了 PropertyMetaData 行:

public static System.Windows.DependencyProperty DepProp
    = System.Windows.DependencyProperty.Register(   "Rect",
                                                    typeof(System.Windows.Shapes.Rectangle),
                                                    typeof(FooControl),
                                                    new System.Windows.PropertyMetadata(new System.Windows.Shapes.Rectangle()));
public System.Windows.Shapes.Rectangle Rect
{
    get
    { return ((System.Windows.Shapes.Rectangle)(GetValue(DepProp))); }
    set
    { SetValue(DepProp, value); }
}  

从对 Register() 的调用中删除 PropertyMetadata 会使程序完美运行,不会出现任何崩溃或任何其他问题。 但我需要后面的代码的默认值。 如何让它接受默认值而不崩溃?

当它崩溃时,输出窗口中会显示以下异常:

A first chance exception of type 'System.ArgumentException' occurred in WindowsBase.dll  
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll  
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll  

我需要尽快使其正常工作,所以任何建议都会很棒!

Why would a dependency-property implementation crash my application when I provide a default value?

This segment of code is in the class declaration for my UserControl object. Everything works fine - it compiles and runs perfectly.

public static System.Windows.DependencyProperty DepProp
    = System.Windows.DependencyProperty.Register(   "Rect",
                                                    typeof(System.Windows.Shapes.Rectangle),
                                                    typeof(FooControl));
public System.Windows.Shapes.Rectangle Rect
{
    get
    { return ((System.Windows.Shapes.Rectangle)(GetValue(DepProp))); }
    set
    { SetValue(DepProp, value); }
}

However, when I add the default value to the dependency property:
The code compiles, but crashes with a fatal exception when it tries to instantiate the UserControl.

For reference, my code now looks like this - with the PropertyMetaData line added:

public static System.Windows.DependencyProperty DepProp
    = System.Windows.DependencyProperty.Register(   "Rect",
                                                    typeof(System.Windows.Shapes.Rectangle),
                                                    typeof(FooControl),
                                                    new System.Windows.PropertyMetadata(new System.Windows.Shapes.Rectangle()));
public System.Windows.Shapes.Rectangle Rect
{
    get
    { return ((System.Windows.Shapes.Rectangle)(GetValue(DepProp))); }
    set
    { SetValue(DepProp, value); }
}  

Removing the PropertyMetadata from the call to Register() causes the program to function perfectly, without any crashes or any other problems. But I need the default value for later code. How can I get it to accept the default value without crashing?

When it crashes, the following exceptions are shown in the output window:

A first chance exception of type 'System.ArgumentException' occurred in WindowsBase.dll  
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll  
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll  

I need to get this working ASAP, so any advice would be awesome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失退 2024-08-03 19:34:52

简短回答:

依赖属性默认值需要是线程安全的(例如从 System.Windows.Freezable 继承),但 System.Windows.Forms.Rectangle 不是。

长答案:

http:// /social.msdn.microsoft.com/forums/en-US/wpf/thread/2cb12481-ef86-40b7-8333-443598d89933/

提示:

如果您使用的是 Visual Studio,它确实有助于让 IDE 正常运行抛出的每个异常。 只需转到“调试”-> “异常”并选中“公共语言运行时异常”“抛出”。

然后,系统会提示您并收到异常消息,在您的情况下,该消息如下所示:“附加信息:‘矩形’属性的默认值无法绑定到特定线程。”

Short answer:

Dependency property default values need to be thread safe (e.g. inherit from System.Windows.Freezable) but System.Windows.Forms.Rectangle isn't.

Long answer:

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/2cb12481-ef86-40b7-8333-443598d89933/

Hint:

If you are using Visual Studio it really helps to let the IDE break on every exception being thrown. Just go to "Debug" -> "Exceptions" and check "Common Language Runtime Exceptions" "Thrown".

Then you'll be prompted and get the exception message which in your case looks like this: "Additional information: Default value for the 'Rect' property cannot be bound to a specific thread."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文