Why Would a Dependency-Property Implementation Crash My Application When I Provide a Default Value?

爱⌒轻易说出口 提交于 2019-12-18 03:39:33

问题


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!


回答1:


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."



来源:https://stackoverflow.com/questions/1168648/why-would-a-dependency-property-implementation-crash-my-application-when-i-provi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!