Skip to main content

Microsoft Silverlight

Unanswered Question Cannot set a custom DependencyProperty to a StaticResourceRSS Feed

(0)

Casper van Dijk
Casper v...

Member

Member

2 points

4 Posts

Cannot set a custom DependencyProperty to a StaticResource

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); }
}

}

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}" />

Casper

 

Casper van Dijk
Casper v...

Member

Member

2 points

4 Posts

Re: Cannot set a custom DependencyProperty to a StaticResource

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.

    xmlns:core="clr-namespace:System;assembly=mscorlib"
    xmlns:sys="clr-namespace:System;assembly=System"
 
    <Application.Resources>
        <
core:String x:Key="mytext"
          xmlns:core="clr-namespace:System;assembly=mscorlib">some text</core:String>
        <
sys:Uri x:Key="myuri"
          xmlns:sys="clr-namespace:System;assembly=System">Resources/ff.png</sys:Uri>
    </
Application.Resources>
 
    <media:MyControl Width="100" Height="75" Source="{StaticResource myuri}" />
 
Casper

woody3k
woody3k

Member

Member

4 points

2 Posts

Re: Cannot set a custom DependencyProperty to a StaticResource

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?

Casper van Dijk
Casper v...

Member

Member

2 points

4 Posts

Re: Re: Cannot set a custom DependencyProperty to a StaticResource

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.

woody3k
woody3k

Member

Member

4 points

2 Posts

Re: Re: Cannot set a custom DependencyProperty to a StaticResource

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.

 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities