Silverlight 3 - Merged Dictionary Rules
Follow these guidelines for trouble-free dictionary merging in Silverlight
Silverlight 3 allows you to use merged dictionaries, but in order to be successful, you have to know and adhere to the unspoken (and barely documented) rules. I fought with this for half a day before stumbling on the solution to my problem. So that you don't suffer the same fate, I present you with said solution. I assume that you're working on a Silverlight (or WPF) application and are familiar with all of the nomenclature I'm about to use, and that you're just here to laugh and point (and that's okay). :)
Here was my situation. I've been working on a SL3 (navigation) app for about a month. When you create a new app of this type, Visual Studio creates a folder in the project called Assets, and in that folder, it places a resource dictionary called Styles.XAML, containing all of the resources necessary to create the default appearance of the templated application. Then in App.XAML, they merge the Styles.XAML dictionary into the application.
I've been working on my (navigation) app for about a month, steadily adding styles, brushes, templates, and other resources to the original dictionary. This morning, I decided to break the (now huge) dictionary into smaller more targetted files. Let's call one Colors, and two others named Cat and Dog. Both Cat and Dog use reosurces in Colors, and the app uses all three.
Here are the rules:
0) Because both Cat and Dog use resources from Colors, BOTH Cat and Dog have to have a MergedDictionaries section, like so:
<ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/AssemblyName;component/Assets/Colors.xaml"/> </ResourceDictionary.MergedDictionaries>Notice the need for "/AssemblyName;component/". You MUST include this, even if the resource dictionary is part of the same assembly. 1) In App.XAML, you need to add all three dictionaries to the existing MergedDictionaries section:
<ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Assets/Dog.xaml"/> <ResourceDictionary Source="Assets/Cat.xaml"/> <ResourceDictionary Source="Assets/Colors.xaml"/> </ResourceDictionary.MergedDictionaries>Notice the ABSENCE of "/AssemblyName;component/". If you include that phrase in App.XAML, your dictionaries will not merge (at least, they didn't for me). Silverlight continues to shine as the fragile house of cards that we all know it to be. Good luck.