Skip to content

Latest commit

 

History

History
82 lines (55 loc) · 6.51 KB

frameworkelement_resources.md

File metadata and controls

82 lines (55 loc) · 6.51 KB
-api-id -api-type
P:Windows.UI.Xaml.FrameworkElement.Resources
winrt property

Windows.UI.Xaml.FrameworkElement.Resources

-description

Gets the locally defined resource dictionary. In XAML, you can establish resource items as child object elements of a frameworkElement.Resources property element, through XAML implicit collection syntax.

-xaml-syntax

<frameworkElement>
  <frameworkElement.Resources>
    oneOrMoreResourceElements
  </frameworkElement.Resources>
</frameworkElement>

-xaml-values

oneOrMoreResourceElements
oneOrMoreResourceElementsOne or more object elements, each of which creates and defines a resource. Each resource property element in a ResourceDictionary must have a unique key (typically x:Key attribute, specified as an attribute, with a string value ). The key is used when values are retrieved from the ResourceDictionary with a {StaticResource} markup extension XAML usage (or code).
## -property-value The current locally defined dictionary of resources, where each resource can be accessed by its key.

-remarks

The primary purpose of the items in a Resources collection is to refer to them from other parts of your XAML, using a {StaticResource} markup extension reference (or the similar {ThemeResource} markup extension reference). If you want to access the Resources collection at run time, you can use the API of the relevant template to query, add, or remove items in the ResourceDictionary.

For more info and examples, see ResourceDictionary and XAML resource references.

A ResourceDictionary is a keyed collection, which is based on an IMap<K,V> template if you are programming with Visual C++ component extensions (C++/CX), or an IDictionary<TKey,TValue> template if you are programming with C# or Microsoft Visual Basic. The API you use in code to work with the dictionary and its items are reflective of the underlying template and thus of the language you're using for your app.

Application also has a Resources property, which can be used to store resources that should be accessible from more than one page in the app. Resources for custom controls can also be stored in a separate XAML file that is created by the default project template of a templated control.

The items that you see in a XAML Resources collection are not necessarily the entirety of XAML-defined resources available at runtime. Other resources are available at runtime, due to the influence of the MergedDictionaries property on a ResourceDictionary. The MergedDictionaries value can introduce other dictionaries such as the resources defined by the system, such as resources from the default XAML control templates. Runtime theme-specific resources are also available from the similar ThemeDictionaries property. If you access a Resources collection at runtime and query for a specific key using the Item indexer or Lookup method, you can access and retrieve these resources. For more info, see ResourceDictionary and XAML resource references. Also, Application.Resources can provide resources that are available for any XAML reference in the app and thus extend the resources in any given FrameworkElement.Resources dictionary.

-examples

This example shows a XAML definition of a simple Resources dictionary that contains one item, a DataTemplate.

[!code-xaml3]

<GridView ItemTemplate="{StaticResource CBTemplate}" .../>

Using XAML resource definitions and resource references is the typical way to use the Resources property. Most of the time XAML alone can handle common resource scenarios. But you also can use the property to access the collection API and thus retrieve resources with runtime code, if that's needed by your scenario. This example shows code access to the Resources property. In this example the Resources property references is inline and immediately followed by an indexer usage that retrieves a ResourceDictionary item with the string key RainbowBrush. Note the explicit cast; the return value for items from the ResourceDictionary is always a nontyped object.

[!code-cppResourceProceduralGet]

[!code-csharpResourceProceduralGet]

[!code-vbResourceProceduralGet]

<Page.Resources>
...
  <LinearGradientBrush x:Key="RainbowBrush">
    <GradientStop Color="Red" Offset="0.05" />
    <GradientStop Color="Orange" Offset="0.23" />
    <GradientStop Color="Yellow" Offset="0.41" />
    <GradientStop Color="Green" Offset="0.59" />
    <GradientStop Color="Blue" Offset="0.77" />
    <GradientStop Color="Purple" Offset="0.95" />
 </LinearGradientBrush>
</Page.Resources>

-see-also

ResourceDictionary, x:Key attribute, ResourceDictionary and XAML resource references, {StaticResource} markup extension, {ThemeResource} markup extension, Application resources and localization sample, XAML overview 1b37-f1a8-44a4-bb8e-1d4de30e7823)