views:

90

answers:

2

Consider a File | New Project of a WPF Application that contains:

  1. A new custom control named CustomControl1
  2. Two new resource dictionaries named Dictionary1 and Dictionary2

Take the generated style out of Generic.xaml and move it to Dictionary2. Then merge Dictionary2 into Dictionary1 and Dictionary1 into Generic like this:

<!--Generic.xaml-->
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/Themes/Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>

<!--Dictionary1.xaml-->
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Dictionary2.xaml"/>
</ResourceDictionary.MergedDictionaries>

Then, add an instance of CustomControl1 into MainWindow's grid. (This part is necessary to reproduce the issue. The project always compiles fine - only at runtime does the issue show up, and the dictionaries must be referenced.)

In Dictionary1.xaml I am merging in another dict in the same folder, so a simple Source="Dictionary2.xaml" works. Yet in Generic.xaml I must use an absolute URI. If I change the above to be Source="Dictionary1.xaml" without the pack://application stuff then I get a XamlParseException caused by an IOException "Cannot locate resource 'dictionary1.xaml'" when it tries to construct the MainWindow.

My Question: What's special about generic.xaml regarding relative URI resolution, and why?

A: 

Just a guess: generic.xaml needs to be accessible from outside assemblies as well, so it's a way to ensure that the resources can be found from anywhere, using absolute URIs. As I said, it's just a stab in the dark, not sure.

Alex Paven
A: 

I just did exactly what you suggested and it worked as a Relative URI...

http://snipt.org/klgpm

All 3 are located in the Themes folders.

EDIT:

Change the Build Action of Generic.xaml to a Resource from a Page

Aaron
It compiles, but you must actually reference it from a window to get the exception. I'll update the question to make this clear.
Scott Bilas
See edit above...
Aaron
In that case, Generic does not get used at all. Resources are embedded directly and not parsed. Xaml files are required to be Pages to be compiled.
Scott Bilas