Extensible Application Markup Language,XAML是一种声明性标记语言。
一、XAML语法概述
1,与XML类似,用尖括号标记元素
<StackPanel> <Button Content="Click Me"/> </StackPanel>
2,特性语法(属性)
<Button Background="Blue" Foreground="Red" Content="This is a button"/>
3,属性元素
<Button> <Button.Background> <SolidColorBrush Color="Blue"/> </Button.Background> <Button.Foreground> <SolidColorBrush Color="Red"/> </Button.Foreground> <Button.Content> This is a button </Button.Content> </Button>
属性元素丰富了元素的属性,属性元素开始标记的语法为 <类型名称.属性名称>
4,集合语法
属性元素的值支持集合元素
<LinearGradientBrush> <LinearGradientBrush.GradientStops> <!-- no explicit new GradientStopCollection, parser knows how to find or create --> <GradientStop Offset="0.0" Color="Red" /> <GradientStop Offset="1.0" Color="Blue" /> </LinearGradientBrush.GradientStops> </LinearGradientBrush>
5,内容属性(?)
<Border> <TextBox Width="300"/> </Border> <!--explicit equivalent--> <Border> <Border.Child> <TextBox Width="300"/> </Border.Child> </Border>
6,文本内容
有少量 XAML 元素可直接将文本作为其内容来处理。 若要实现此功能,必须满足以下条件之一: 类必须声明一个内容属性,并且该内容属性必须是可赋值给字符串的类型(该类型可以是 Object)。 例如,任何 ContentControl 都将 Content 用作其内容属性,并且其类型为 Object,这样就支持实际的 ContentControl(例如,Button)上的如下用法:<Button>Hello</Button>。 类型必须声明一个类型转换器,该类型转换器将文本内容用作其初始化文本。 例如,<Brush>Blue</Brush>。 这种情况实际上并不常见。 类型必须为已知的 XAML 语言基元。
7,内容属性和集合语法的组合(就是我们常见的UI组织形式)
<StackPanel> <Button>First Button</Button> <Button>Second Button</Button> </StackPanel>
Omitted StackPanel.Children property element:StackPanel derives from Panel.‘ data-guid="d18b29caff08089c3f03a2d2d1d6c153">省略的 StackPanel.Children 属性元素: StackPanel 从 Panel 派生。
<StackPanel> <StackPanel.Children> <!--<UIElementCollection>--> <Button>First Button</Button> <Button>Second Button</Button> <!--</UIElementCollection>--> </StackPanel.Children> </StackPanel>
8,特性语法(事件)
特性语法还可用于事件成员,而不仅限于属性成员。 在这种情况下,特性的名称为事件的名称。 在 XAML 事件的 WPF 实现中,特性的值是实现该事件的委托的处理程序的名称。
<Button Click="Button_Click" >Click Me!</Button>
二、标记扩展
标记扩展是一个 XAML 语言概念。 { and }) indicate a markup extension usage.‘ data-guid="afd6b522816021e65f83030354cd300b">当用于提供特性语法的值时,大括号({ 和 })表示标记扩展用法。 此用法指示 XAML 处理系统不要像通常那样将特性值视为一个文本字符串或者可转换为字符串的值。
常用标记扩展: Binding(用于数据绑定表达式)以及资源引用 StaticResource 和 DynamicResource
以下面的代码为例,Style的值是一个Style类型的实例,实例名称是Page.Resources里Sytle的Key。StaticResource.‘ data-guid="28bad1bdbe8ab66821c73f7f41c8aa0f">引用了特定的标记扩展 StaticResource。 当处理该标记扩展时,它返回对以前在资源字典中作为键控资源进行实例化的某个样式的引用。
<Page.Resources> <SolidColorBrush x:Key="MyBrush" Color="Gold"/> <Style TargetType="Border" x:Key="PageBackground"> <Setter Property="Background" Value="Blue"/> </Style> ... </Page.Resources> <StackPanel> <Border Style="{StaticResource PageBackground}"> ... </Border> </StackPanel>
三、类型转换器
下面额Margin之所以可以这么写,是因为有类型转换器。
<Button Margin="10,20,10,30" Content="Click me"/>
下面是更详细的语法
<Button Content="Click me"> <Button.Margin> <Thickness Left="10" Top="20" Right="10" Bottom="30"/> </Button.Margin> </Button>
Thickness structure is an example of a type that has a type conversion enabled for XAML usages.‘ data-guid="6fd9fa9268b11178569799084356119a">怎么实现的呢?Thickness 结构是一个类型示例,该类型拥有可使用 XAML 的类型转换。 Thickness indicates measurements within a nested rectangle and is used as the value for properties such as Margin.‘ data-guid="1fa13bdad255ee211d6884c50d2f2714">Thickness, all properties that use a Thickness are easier to specify in XAML because they can be specified as attributes.‘ data-guid="22d8f606e03df5113cda64fc3dc1d226">通过对 Thickness 设置类型转换器,所有使用 Thickness 的属性都可以更容易地在 XAML 中指定。
原文:http://www.cnblogs.com/LLLLoveLLLLife/p/3546382.html