Delen via


XAML Markup Extensions

XAML markup extensions help extend the power and flexibility of XAML by allowing element attributes to be set from sources other than literal text strings.

For example, normally you set the Color property of BoxView like this:

<BoxView Color="Blue" />

Or, you can set it to a hexadecimal RGB color value:

<BoxView Color="#FF0080" />

In either case, the text string set to the Color attribute is converted to a Color value by the ColorTypeConverter class.

You might prefer instead to set the Color attribute from a value stored in a resource dictionary, or from the value of a static property of a class that you've created, or from a property of type Color of another element on the page, or constructed from separate hue, saturation, and luminosity values.

All these options are possible using XAML markup extensions. But don't let the phrase "markup extensions" scare you: XAML markup extensions are not extensions to XML. Even with XAML markup extensions, XAML is always legal XML.

A markup extension is really just a different way to express an attribute of an element. XAML markup extensions are usually identifiable by an attribute setting that is enclosed in curly braces:

<BoxView Color="{StaticResource themeColor}" />

Any attribute setting in curly braces is always a XAML markup extension. However, as you'll see, XAML markup extensions can also be referenced without the use of curly braces.

This article is divided in two parts:

Consuming XAML Markup Extensions

Use the XAML markup extensions defined in Xamarin.Forms.

Creating XAML Markup Extensions

Write your own custom XAML markup extensions.