3

In my Silverlight app I have UserControl and I want to reference a StaticResource in a ResourceDictionary that is in a separate XAML file.

My UserControl looks like this:

<UserControl x:Class="ResourceDictionaryHeadache.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
    <UserControl.Resources>
        <ResourceDictionary Source="/SampleData.xaml" />
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot">
        <ListBox HorizontalAlignment="Stretch"
                 VerticalAlignment="Stretch"
                 ItemsSource="{StaticResource SampleData}">
        </ListBox>
    </Grid>
</UserControl>

My SampleData.xaml file looks like this:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:Headache="clr-namespace:ResourceDictionaryHeadache">
<Headache:PersonList x:Key="SampleData">
    <Headache:Person Name="Joe" Age="20" />
    <Headache:Person Name="Sam" Age="25" />
    <Headache:Person Name="Dave" Age="30" />
</Headache:PersonList>

I have the SampleData.xaml file set to a Build Action of Content and when I run the app I get an AG_E_PARSER_BAD_TYPE [Line: 5 Position: 44] error on the InitializeComponent() line of the constructor for my UserControl.

What is causing this error and how can I correctly reference this resource?

joshuapoehls
  • 32,695
  • 11
  • 50
  • 61
  • Sometimes setting Common Language Runtime Exceptions in VS to catch unhandled exceptions you can get it to give you more hints on whats actually blowing up. (Press control-alt-E, then check Thrown next to CLRE) – Stephen Price Jan 08 '10 at 05:51

2 Answers2

4

Set the Build Action to Resource and then reference it like below:

<ResourceDictionary Source="/AssemblyName;component/sampledata.xaml" />

make sure that its all in lowercase from component onwards as thats how it ends up in the resources of the dll.

Stephen Price
  • 1,629
  • 1
  • 24
  • 42
  • How does this help anything. It doesn't really make sense to bury XAML as resource in the assembly, its much more natural to have it as another entry in the XAP – AnthonyWJones Jan 08 '10 at 09:22
  • I'll give this a shot tonight. Does "component" always have to be literally "component" or should it be the subfolder that sampledata.xaml is in within the project? -- Also, I agree a bit with Anthony here that at least in my scenario I'd much rather have the XAML as 'Content' instead of a resource in the assembly. Its driving me nuts because I've seen so many examples that use it this way (as Content) and it just doesn't seem to be working for me here... – joshuapoehls Jan 08 '10 at 13:50
  • This actually worked when I was trying to repro your problem. Yes, "component" literally has to be "component"... you do not need a subfolder. I don't have a good explanation for why this is, but I have a feeling it has something to do with your ResourceDictionary including a custom type and the timing of when the ResourceDictionary is instantiated using the two different methods. The path to the XAML file is clearly correct, the parser is just choking on your custom PersonList type using the Content method, but it is somehow available when using the Resource method. – Dan Auclair Jan 08 '10 at 17:17
  • I had the same problem only after upgrading to VS2010 SP1. Anyway this fixed it for me, thanks – Dale Feb 25 '13 at 04:19
2

This line in your resource dictionary doesn't look right to me:-

 xmlns:Headache="clr-namespace:ResourceDictionaryHeadache"

Is your PersonList class really defined in a Namespace called ResourceDictionaryHeadache?

Whether it is or isn't I suspect that the reason the code it failing is because XAML can't find the PersonList type.

Edit

D'Oh! I just noticed, remove the preceding / from the Source and leave the SampleData.xaml Resource dictionary at its default Build Action of "Page".

In other words if you just added the XAML file using "Add New Item" then "Resource Dictionary" you would only need this in your page xaml:-

<UserControl.Resources> 
    <ResourceDictionary Source="SampleData.xaml" /> 
</UserControl.Resources>
AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • That is the correct namespace. The PersonList class is shown here: http://gist.github.com/271809. Also, if I copy the contents of that ResourceDictionary file directly into the UserControl's resources then it works just fine. Unless being in a separate file means the ResourceDictionary has access to different things I don't see how that can be the issue. – joshuapoehls Jan 08 '10 at 13:48