If you ever worked with a larger XAML Project the entries in your resource dictionaries will start to pile up and it’s really a pain to find where you put some Brush or Style or whatever.
Wouldn’t it be nice if you could separate your resources into different kinds and manage them in there own files? Well of course you can – and it’s very easy but there are some steps to remember.
I like having my resource dictionaries in a separate folder within my projects – I guess most people call this “Assets” but name mine “Resources” (go figure) – so first create a new folder with whatever name you want and put a new “Resource Dictionary” in there:
(Note I’m using the VS11 beta but it’s exactly the same in VS2010)
The file opens like this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> </ResourceDictionary>
And you can begin filling your resources in there (of course there is support in Blend for this too).
The next step is telling your main resource-dictionary (usually in the App.xaml file) to merge with your newly created dictionary. This is done using something like the following:
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary> <Color x:Key="Redish" R="155" /> </ResourceDictionary> <ResourceDictionary Source="Resources/Templates.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>
As you can see – all that there is to it is using the ResourceDictionary.MergedDictionaries collection and adding dictionaries. In this case I use one “in-place” and the Templates dictionary I created in the last step (setting the Source property).
That’s it – now you can use those resources as if you had defined them in your local or App resources.