Click here to Skip to main content
15,885,782 members
Articles / Silverlight
Tip/Trick

Silverlight 3 - Merged Dictionary Rules

Rate me:
Please Sign up or sign in to vote.
4.75/5 (5 votes)
21 May 2010CPOL2 min read 16.5K   5   2
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
GeneralThis article is great. Pin
felipe20514-Apr-13 1:26
felipe20514-Apr-13 1:26 
GeneralI wish I could give a 6 for this...5 for the tip and +1 for ... Pin
TheyCallMeMrJames18-May-10 5:11
TheyCallMeMrJames18-May-10 5:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.