XAML マークアップ拡張XAML Markup Extensions
サンプルのダウンロード
Download the sample
XAML マークアップ拡張機能は、リテラルテキスト文字列以外のソースから要素属性を設定できるようにすることで、XAML のパワーと柔軟性を拡張するのに役立ちます。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.
たとえば、通常、次 Color
のようにのプロパティを設定し BoxView
ます。For example, normally you set the Color
property of BoxView
like this:
<BoxView Color="Blue" />
または、16進数の RGB 色の値に設定することもできます。Or, you can set it to a hexadecimal RGB color value:
<BoxView Color="#FF0080" />
どちらの場合も、属性に設定されたテキスト文字列 Color
は、 Color
クラスによって値に変換され ColorTypeConverter
ます。In either case, the text string set to the Color
attribute is converted to a Color
value by the ColorTypeConverter
class.
代わりに、 Color
リソースディクショナリに格納されている値、または作成したクラスの静的プロパティの値、またはページ上の別の要素の型のプロパティから、また Color
は別の色相、鮮やかさ、明度の値から構築された属性を設定することをお勧めします。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.
これらのオプションはすべて、XAML マークアップ拡張機能を使用して実行できます。All these options are possible using XAML markup extensions. しかし、"マークアップ拡張機能" という語句を除いて、XAML マークアップ拡張機能は XML の拡張機能では ありません 。But don't let the phrase "markup extensions" scare you: XAML markup extensions are not extensions to XML. Xaml マークアップ拡張機能を使用している場合でも、XAML は常に有効な 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 マークアップ拡張機能は、中かっこで囲まれた属性設定で識別できます。XAML markup extensions are usually identifiable by an attribute setting that is enclosed in curly braces:
<BoxView Color="{StaticResource themeColor}" />
中かっこ内のすべての属性設定は、 常に XAML マークアップ拡張機能です。Any attribute setting in curly braces is always a XAML markup extension. ただし、このように、中かっこを使用せずに XAML マークアップ拡張機能を参照することもできます。However, as you'll see, XAML markup extensions can also be referenced without the use of curly braces.
この記事は、次の2つの部分で構成されています。This article is divided in two parts:
XAML マークアップ拡張の使用Consuming XAML Markup Extensions
「」で定義されている XAML マークアップ拡張機能を使用し Xamarin.Forms ます。Use the XAML markup extensions defined in Xamarin.Forms.
XAML マークアップ拡張の作成Creating XAML Markup Extensions
独自のカスタム XAML マークアップ拡張機能を作成します。Write your own custom XAML markup extensions.