This is the Title of the Book, eMatter Edition
Copyright © 2008 O’Reilly & Associates, Inc. All rights reserved.
Chapter 5Resources
Every XAML element has a collection of resources. Resources provide a mecha-
nism for defining common styles or elements that can be reused throughout the
user interface. They also configure the actions that are carried out when a user
interacts with a display element.
The benefit of using resources to define reusable, common styles is that modifica-
tions can be applied to one element, but they will take effect throughout the entire
application. This reduces the chance of error and the possibility that an element
might be missed when changes are applied. For example, you may want to define
a specific
Point from which all geometric shapes will originate. By defining the
Point as a resource and referencing it as the appropriate attribute value of
geometric elements, the origination point can easily be changed in one place—the
resource declaration—without concern for mistakes made in multiple places
throughout the user interface.
Local resources are defined on the element, while global resources are defined on
the root element. Global resources can be used by all elements in the page while
local resources are reserved for use by the element in which they are declared.
Regardless of the type of resource (local or global), the syntax used to declare the
resources is the same.
Although every element has a collection of resources, they are usu-
ally declared only on the root element.
Using Resources
When adding resources, you must add the appropriate namespace to the root
element. You’ll also need to give it a name to differentiate it from the default
namespace. The default namespace, which references Avalon, contains the
Chapter 5: Resources
This is the Title of the Book, eMatter Edition
Copyright © 2008 O’Reilly & Associates, Inc. All rights reserved.
definitions of Avalon elements, such as Button, Page, and StackPanel. The
namespace that must be added to define resources is the XAML namespace and
describes the language itself.
Because resource definitions require the use of XAML-specific tags—which are
not described by the default namespace—you must declare a reference to the
XAML namespace and use it to prefix those attributes found only there, such as
The key is a fully qualified attribute comprising the namespace, a colon, and the
Key. Elements defined as a resource must have a declared “key name” to
be referenced by other elements. The value of the attribute is the name by which
the resource will be referenced by other elements.
Resources are added by explicitly declaring elements as children of the Resources
attribute of an element.
In Example 5-1, there are two instances of
SolidColorBrush defined as resources:
RedBrush and BlueBrush.
Resources must be declared in the file before they can be accessed. This is because
the runtime engine interprets XAML as a stream of binary input and doesn’t
understand that a resource might be defined later in the stream. It can’t render an
element if a resource is required but hasn’t yet been declared. In Example 5-2, the
RedBrush is referenced before it is declared.
Example 5-1. Using resources to define global styles
Background="{StaticResource RedBrush}" />
Fill="{StaticResource BlueBrush}"
Example 5-2. Illegal use of a local resource
Content="Click Me"
Background="{StaticResource RedBrush}" >

Get XAML in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.