Skip to main content
Home Forums Silverlight Programming Report a Silverlight Bug Cannot set a custom DependencyProperty to a StaticResource
4 replies. Latest Post by woody3k on June 24, 2009.
(0)
Casper v...
Member
2 points
4 Posts
05-27-2009 5:47 AM |
Hi, I added a custom DependencyProperty of type Uri (with the UriTypeConverter) to my custom control. Now I want to set its value in XAML to a static resource. This fails with AG_E_PARSER_BAD_PROPERTY_VALUE. Does anyone know if this is a bug or a feature?
My code:
public class MyControl : Control{ public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(Uri), typeof(MyControl), new PropertyMetadata(OnSourcePropertyChanged)); [TypeConverter(typeof(UriTypeConverter))]public Uri Source{ get { return (Uri)GetValue(SourceProperty); } set { SetValue(SourceProperty, value); }}
[TypeConverter(
}
This works fine in XAML:
<media:MyControl Width="100" Height="75" Source="Resources/ff.png" />
This fails in XAML (Visual Studio complains about AG_E_PARSER_BAD_PROPERTY_VALUE):
<Application.Resources><sys:String x:Key="mediaKey" xmlns:sys="clr-namespace:System;assembly=mscorlib">Resources/ff.png</sys:String></Application.Resources> <media:MyControl Width="100" Height="75" Source="{StaticResource mediaKey}" />
<Application.Resources><sys:String x:Key="mediaKey" xmlns:sys="clr-namespace:System;assembly=mscorlib">Resources/ff.png</sys:String></Application.Resources>
<media:MyControl Width="100" Height="75" Source="{StaticResource mediaKey}" />
Casper
06-02-2009 11:28 AM |
Hmm answering my own post, tacky.. for those who run into the same problem, there is a simple workaround (thanks Coolio):
Dont define the resources as string, define them as Uri. Note that System.Uri is in System.dll, not in mscorlib.
woody3k
4 points
2 Posts
06-23-2009 8:01 PM |
Hi,
I have found myself in the same situation you initially described - but I find that I cannot even successfully declare a Uri in the Application Resources - I get a nice AG_E_PARSER_BAD_PROPERTY_VALUE with line & character pointing to the <sys:Uri> part of the element. I can reproduce with minimal addition to a new project (VS2008SP1 with latest SL Tools):
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="SilverlightApplication1.App" xmlns:sys="clr-namespace:System;assembly=System" > <Application.Resources> <sys:Uri x:Name="str">http://silverlight.net</sys:Uri> </Application.Resources></Application>
Adding the namespace to the Uri element doesn't help either.
You're not using SL3 are you? Or maybe have additional namespaces in the Application header?
06-24-2009 3:52 AM |
To make an added namespace work in the Application.Resources section, it seems that you need to repeat the namespace declaration in the element itself:
<sys:Uri x:Key="str" xmlns:sys="cls-namespace:System;assembly=System">bla</sys:Uri>
Then it works for me.. I am still on SL2.
06-24-2009 8:52 PM |
Mysterious, I tried the same but no luck. Have gone through the usual reinstall latest SL Tools and Dev runtime (2.0.40115.0) to no effect, only thing I can think of is that I'm running VS2008 Standard, not Pro - maybe SL Tools reacts differently to this.
Definitely would seem to be the xaml parser that is complaining here, error only generated when the xaml file with this resource in is Loaded at runtime.
I can add an identical Uri resource to App.Current.Resources in the App ctor which works fine, can use it as a staticresource.
Not a critical issue to solve for me given that there is an easy workaround, was just an interesting problem which seems to work for some people but not others.