views:

972

answers:

2

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?

+2  A: 

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
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
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...
Joshua Poehls
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
A: 

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
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.
Joshua Poehls
Removing the / worked! Thank you! I can't believe I hadn't tried that already...
Joshua Poehls