Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
839 views
in Technique[技术] by (71.8m points)

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

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...