Search code examples
c#wpfuriresourcedictionarymergeddictionaries

XAML resource handling slowly driving me crazy


I am working with loose XAMLs (Build Action None, but always copy to output directory). Also i have a "root" xaml that is a resource dictionary which references some merged dictionaries which are located in a subfolder. This is my folder structure:

<root>\Root.xaml
<root>\SubFolder1\MergedDict1.xaml
<root>\SubFolder1\MergedDict2.xaml
<root>\SubFolder2\MergedDict3.xaml
<root>\SubFolder2\MergedDict4.xaml

I need to be able to add additional xaml files to the folders without recompiling, that is why i need to work with loose xamls.

In the application i load the root xaml, but always get the exception that the merged dictionaries cannot be found. I have tried a lot of different variants of assigning relative path as a source. Some of those include:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="<root>/SubFolder1/MergedDict1.xaml" />
    <ResourceDictionary Source="/SubFolder1/MergedDict1.xaml" />
    <ResourceDictionary Source="SubFolder1/MergedDict1.xaml" />
    <ResourceDictionary Source="./SubFolder1/MergedDict1.xaml" />
    <ResourceDictionary Source="../SubFolder1/MergedDict1.xaml" />
...

To my surprise (and ongoing horror) none of them have worked.

I started questioning my sanity so i have even written a small test application to generate the relative path for me, using URIs:

        System.Uri uri2 = new Uri(@"C:\<root>\SubFolder1\MergedDict1.xaml");
        System.Uri uri1 = new Uri(@"C:\<root>");

        var r = uri1.MakeRelativeUri(uri2).ToString();

The generated relative path was, as expected, SubFolder1/MergedDict1.xaml, did also not work. Who can teach me the black magic involved in getting the correct relative path?


Solution

  • It looks like your xaml files are all in the same component. Still, using pack URIs may help: http://msdn.microsoft.com/en-us/library/aa970069.aspx