#53 – Accessing Application-Scoped Resources from Code

You can access application-scoped resources from code by using the Application.Resources property.  The property points to a ResourceDictionary that contains a collection of DictionaryEntry objects.

For resources defined in XAML, the key of each entry is a string and the value is an object of the associated resource type.

For example, for the SolidColorBrush resource shown below:

 <Application.Resources>
     <SolidColorBrush x:Key="greenBrush"  Color="Green"/>
 </Application.Resources>

The dictionary entry’s Key is the string “greenBrush” and the Value is a SolidColorBrush object with the Color property set to green.

 SolidColorBrush br = (SolidColorBrush)Application.Current.Resources["greenBrush"];
Advertisements