Uri property in a Silverlight 2 UserControl


In creating a Silverlight 2 UserControl, I had a need to include a Uri parameter, so I added a property. 

class MyControl : UserControl
{
    ...
    public Uri Source {get;set;}
    ...
}

That compiled and ran just fine but when I tried to set the value via XAML, I got an AG_E_PARSER_BAD_PROPERTY_VALUE error.  The property was missing a type converter, a class that converts between a string and the property value.  I added a using directive and the attribute to the property specifying to use the built-in UriTypeConverter:

using System.ComponentModel;

class MyControl : UserControl
{
    ...
    [TypeConverter(typeof(UriTypeConverter))]
    public Uri Source {get;set;}
    ...
}

That fixed it!

Comments (4)

  1. Mark Monster on cross-comain-scripting issues, Page Brooks on his treadmill, Mike Snow on the Mouse Wheel

  2. shinyzhu says:

    That’s resolved my problem too. Thanks.

  3. Anna says:

    Thanks for the advice! I just used a string so far.