FrameworkElement.Resources property

Applies to Windows and Windows Phone

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.

Syntax


public:
property ResourceDictionary^ Resources { 
   ResourceDictionary^ get();
   void set (ResourceDictionary^ value);
}


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

XAML Values

oneOrMoreResourceElements

One 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, specified as an attribute, with a string value ). The key is used when values are retrieved from the ResourceDictionary with a StaticResource XAML usage (or code).

Property value

Type: ResourceDictionary

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 reference (or the similar ThemeResource reference). If you want to access the Resources collection at run time, you can use the APIs 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 APIs 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.


<Grid.Resources>
  <DataTemplate x:Key="CBTemplate">
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
      </Grid.ColumnDefinitions>
      <Image Grid.Column="0" Width="50" Height="50" 
        Source="{Binding Photo}" Stretch="Fill"/>
      <TextBlock Grid.Column="1" Text="{Binding Title}" 
        Margin="10" HorizontalAlignment="Left" FontSize="20"/>
    </Grid>
  </DataTemplate>
</Grid.Resources>


You'd then reference this resource from another part of your XAML, referring to it by its key and using the StaticResource markup extension:


<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 APIs 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.


    void MainPage::SetBGByResource(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
      Button^ b = safe_cast<Windows::UI::Xaml::Controls::Button^>(sender);
      b->Background = safe_cast<Windows::UI::Xaml::Media::Brush^>(this->Resources->Lookup("RainbowBrush"));
    }


The RainbowBrush resource is declared in XAML like this, assuming a Page as the root element:


<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>

Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.UI.Xaml
Windows::UI::Xaml [C++]

Metadata

Windows.winmd

See also

FrameworkElement
ResourceDictionary
x:Key
ResourceDictionary and XAML resource references
StaticResource
ThemeResource
Application resources and localization sample
XAML overview

 

 

Show:
© 2014 Microsoft