Tuesday, August 5, 2008

A Silverlight 2 Beta 2 Bug related to Uri type

Again I struck in a framework bug which doesn't allow us to declare and use a dependency property of Type Uri.ie If you declare a dependency property of type Uri and try to set from xaml , will throw an error at runtime.Here is scenario.

You are creating a custom control and you need to have a dependency of type Uri Named Source.

You declared as follows.

public Uri Source
get { return (Uri)GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
public static readonly DependencyProperty SourceProperty =
DependencyProperty.Register("Source", typeof(Uri), typeof(MediaItem), new PropertyMetadata(OnSourceChanged));

static void OnSourceChanged(object sender, DependencyPropertyChangedEventArgs args) { }

Fine.But If you create its instance in xaml as follows and run the application it will throw an error.

<ctrl:MediaItem x:Name="item" Source="dock.jpg" />

Microsoft has identified this error and rectified.Details here
Until the next version change the property type into string and use converters in xaml.

Or in the setter of a dummy property convert the value into Uri and keep in actual property.