Resource dictionaries that can be defined completely or partially in XAML are typically created through a XAML property element for the Resources property, and are typically on the root for any individual page in an application or for the application object. Placing the resource dictionary at this level makes it easier to reference resources in scope from individual child objects in the object tree defined in a XAML page.
Resources created from a XAML-defined resource dictionary can be referenced by code by calling the ResourceDictionary API after you get the value for Resources, but be aware that resources created in XAML will definitely not be accessible until after Loaded is raised by the object that declares the dictionary.
The underlying ResourceDictionary type of the property's value supports the members that are required to add, remove or query resources from within the dictionary by using code.
Notice that the XAML syntax shown does not include an object element for the ResourceDictionary class. This is an example of XAML implicit collection syntax; a tag representing the collection element can be omitted. The elements that are added as items to the collection are specified as child elements of a property element of a property whose underlying type supports a collection Add method.
If you intend to use resources from XAML markup, you should map the XML namespace for XAML through an xmlns declaration. This is necessary because resources defined in markup must each have a unique key value. Key is defined in the XAML namespace, and Key is generally the way to specify a resource key for Silverlight, except for in some special scenarios. Mapping the XML namespace for XAML is already done for you by most (if not all) tools that produce XAML for Silverlight.
Note: |
|---|
The Silverlight documentation assumes the typical mapping prefix of x: for the XAML namespace, therefore references to "Key" in the documentation are typically in the form x:Key, with the x prefix. Also, it is typical to map both the default Silverlight namespace and the XAML namespace (to x: prefix) at the root level. |