Gotcha: Silverlight DependencyProperty metadata: 0d not 0 for double
Just came accross a runtime error message that troubled me a bit to resolve while adding Prezi-like content scaling functionality in ClipFlair‘s FloatingWindowHostZUI (ZUI = Zoomable User Interface) container.
I had added the following:
/// Identifies the <see cref="FloatingWindow.Scale" /> dependency property. /// </summary> /// <value> /// The identifier for the <see cref="FloatingWindow.Scale" /> dependency property. /// </value> public static readonly DependencyProperty ScaleProperty =
DependencyProperty.Register(
"Scale",
typeof(double),
typeof(FloatingWindow),
new PropertyMetadata(0d, OnScalePropertyChanged));
in the context of adding a dependency property (with some extra code for OnScalePropertyChanged static method and for Scale property that calls GetValue/SetValue methods of Control’s far ancestor DependencyObject to update the ScaleProperty).
It seems that the issue was that I was setting a default value of 0 (an integer in C#) instead of 0d (a double – for float type one would use 0f instead). The strange exception returned was saying:
Failed to create a ‘System.Windows.DependencyProperty’ from the text ‘Width’. [Line: 10 Position: 44]
and was pointing me to line 24 of FloatingWindow.cs (obviously the line 10 mentioned in the message meant in some XAML file, but failed to mention which XAML file – or at least say “near …” copying some part the context of the XAML stream, similar to how some server-side scripting languages do).
Clicking “View detail” on the (bad in terms of usability) exception dialog of Visual Studio, I saw in another window a XamlParseException with the following content (alternatively one could expand the tree shown there and examine just the InnerException node’s content there):
{System.Windows.Markup.XamlParseException: Failed to create a ‘System.Windows.DependencyProperty’ from the text ‘Width’. [Line: 10 Position: 44] —> System.TypeInitializationException: The type initializer for ‘SilverFlow.Controls.FloatingWindow’ threw an exception. —> System.ArgumentException: Default value type does not match type of property.
at System.Windows.DependencyProperty.Register(Boolean fIsAttachedDP, String name, Type propertyType, Type ownerType, PropertyMetadata propertyMetadata, Boolean readOnly)
at System.Windows.DependencyProperty.Register(String name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata)
at SilverFlow.Controls.FloatingWindow..cctor()
— End of inner exception stack trace —
at System.Runtime.CompilerServices.RuntimeHelpers._RunClassConstructor(RuntimeType type)
at System.Windows.DependencyProperty.QueryRegisteredProperty(String name, Type ownerType)
at MS.Internal.ManagedTypeInfoProviderRPInvokes.LookupDependencyProperty(Type type, String name)
at MS.Internal.ManagedTypeInfoProviderRPInvokes.ResolveDependencyPropertyName(XamlTypeToken sTypeToken, String inPropertyName, XamlPropertyToken& outProperty, XamlTypeToken& outPropertyTypeToken)
— End of inner exception stack trace —
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at FloatingWindowZUI.Demo.App.InitializeComponent()
at FloatingWindowZUI.Demo.App..ctor()}
Now why it was saying "from the text Width", I guess it’s something in the XAML template of the control, but I really am not sure – it sure was very misleading to say that, since it had nothing to do with the issue.
Moreover DependencyProperty.Register could have been implemented better to force implicit type conversions in such cases (by having several overloaded versions to accept primitive datatypes such as float and double – probably its a victim of the effort to keep Silverlight installer small).
It seems this design is the reason DependencyProperty snippets usually have an explicit type cast at call PropertyMetadata constructor, e.g. one could also use “new PropertyMetadata((double)0, …” instead of “new PropertyMetadata(0d, …”.
Update:
BTW, using 0d as the default value for a Scale property as I originally was doing above isn’t a good idea, since scale factor is practically a multiplier and you can get really strange results. Better use 1d (scale factor 1) which is more logical after all as a default scale value.
-
2012/08/15 at 19:02 | #1HowTo: Scale control arround its center using a render transform « George Birbilis @zoomicon
Leave a Reply Cancel reply
Recent Posts
- Vita: ενεργειακά βραχιολάκια και χάντρες για τους ιθαγενείς
- .NET String extension methods to check for array of prefixes or suffixes
- .NET String class extensions to replace prefix or suffix
- Fix: remove ‘optimized for Bing and MSN’ from IE titlebar
- IIS FTP login fails after Windows platform update for Server 2008 R2
- HowTo: format XML output of DataContractSerializer
- Fix: Silverlight Media Framework Player VolumeElement out of sync
- Fix: Visual Studio opens class diagram in XML editor with double click
- 2012 in review for @zoomicon blog
- Eco-unfriendly Philips shipping empty software CD with just a URL in it
Categories
Archives
- April 2013
- March 2013
- February 2013
- December 2012
- November 2012
- October 2012
- September 2012
- August 2012
- July 2012
- June 2012
- May 2012
- April 2012
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- July 2011
- June 2011
- May 2011
- March 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- July 2006