How to: Use an Application-Scope Resource Dictionary
This example shows how to use an application-scope custom resource dictionary.
Application exposes an application-scope store for shared resources: Resources. By default, Resources is initialized with an instance of the ResourceDictionary type. You use this instance when you get and set application-scope properties using the Resources property (see How to: Get and Set Application-Scope Resources).
If you have multiple resources that you set using Resources, you can instead use a custom resource dictionary to store those resources and set Resources with it instead. You declare a custom resource dictionary using markup, like so:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > <SolidColorBrush x:Key="StandardSolidColorBrush" Color="White" /> <LinearGradientBrush x:Key="StandardLinearGradientBrush" StartPoint="0.0,0.0" EndPoint="1.0,1.0"> <LinearGradientBrush.GradientStops> <GradientStop Color="White" Offset="0" /> <GradientStop Color="Black" Offset="1" /> </LinearGradientBrush.GradientStops> </LinearGradientBrush> </ResourceDictionary>
Swapping entire resource dictionaries using Resources allows you to support application-scope, themes, where each theme is encapsulated by a single resource dictionary. See Themed Application Sample.
To get application-scope resources from the resource dictionary exposed by Resources, whether created by Application or your, requires code like the following:
// Get an application-scope resource Brush whiteBrush = (Brush)Application.Current.Resources["ApplicationScopeResource"];
There are two considerations to make when using Resources. First, the dictionary key is an object, so you need to use exactly the same object instance when both setting and getting a property value (note that the key is case-sensitive when using a string). Second, the dictionary value is an object, so you will need to convert the value to the desired type when getting a property value.