Resources in XAML

Efficient developers like to create capabilities once, and then reuse them as necessary. The usual object-oriented techniques for reuse are available in XAML, because the .NET Framework provides them. But XAML also includes a special capability to create constructs for reuse, in the form of XAML resources.

If you are an experienced .NET developer, the term resources may already be familiar to you. .NET supports resources for capabilities such as localization. Resource files in .NET normally have the extension .resx. However, that's a completely separate concept from XAML resources. This section has nothing to do with .resx files, localization, or any of the subjects you might normally associate with .NET resources.

In XAML, the term resources has a different meaning. XAML resources are objects that are declared once and then available for use in several places. Resources are usually declared in XAML, but as with all other XAML objects, they can also be created in code. In this chapter, you will work exclusively with XAML resources created in XAML.

The Resources Property

XAML elements have a property named Resources, and it holds a dictionary of objects called a ResourceDictionary. Any type of object can go in the dictionary.

Because it's a dictionary, every item in a ResourceDictionary needs a key. This key is normally supplied with another helper capability in the x: namespace, which is the x:Key attribute.

To see the syntax, suppose our UserControl in the original example needed to have a linear gradient brush that would be used for multiple elements. Syntax to declare such a brush in the UserControl's resources dictionary looks like this:

<UserControl.Resources>
    <LinearGradientBrush x:Key="ReusableBrush"
                         EndPoint="1,0.5" StartPoint="0,0.5">
        <GradientStop Color="Blue" Offset="0" />
        <GradientStop Color="Turquoise" Offset="1" />
    </LinearGradientBrush>
</UserControl.Resources>

This brush can then be applied to any property that takes a brush, for any element in the UserControl. The syntax for applying the brush to a Button's Foreground property looks like this:

<Button Margin="5" Click="Button_Click" 
        Foreground="{StaticResource ReusableBrush}">
    I'm a button
</Button>

When a property is set to a resource, the resource can be located on that element, its parent, or any ancestor of the element in the tree. For resources that need to be shared among all visual surfaces in an application, there is a Resources property for the Application object. The Application object is declared in a XAML file named Application.xaml in WPF, or App.xaml in Silverlight or WinRT.

More about Resource Dictionaries

Beginning XAML developers often do not realize that the largest portion of XAML in a typical application is located in various resource dictionaries. Styles and control templates, covered later in this chapter, are normally placed in resource dictionaries.

A ResourceDictionary can hold any type of object, and reusable nonvisual objects are also often placed there. You'll see an example in the next section on data binding when we discuss value converters.

In addition to being places in the Resources property, as in the previous UserControl example, ResourceDictionary collections can also be placed in their own dedicated XAML files for sharing among multiple applications. The syntax for that is beyond the scope of this chapter, but examples of that syntax are found in the help files for the ResourceDictionary class.

Scope of a Resource

As mentioned, a resource is located by going up the tree through parents of the current element. As soon a resource matching a resource key is found, the search stops.

That means that it's possible to “override” resources. If you have a resource named “StandardForegroundBrush” at the application level, it can be used throughout the application. But suppose in a single UserControl, another resource is defined in the UserControl's Resources property with the same name of “StandardForegroundBrush.” Elements in that UserControl use the local version of the resource.

Elements in other parts of the application will not encounter that resource in their search for “StandardForegroundBrush,” so they will use the one at the application level instead.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset