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

Get Professional Visual Basic 2012 and .NET 4.5 Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.