XAML,既然用户控件是一个大类,那么干脆对这个集合进行统一的编程模式,这就是xaml的原义。同时xaml的attribute programming是微软倡导式编程革命,enjoy this!
xmlns是xml形式的namespace,用于引入名字空间。见下例:
<Page xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”>
<Button x:Name=”blueButton” Width=”100” Height=”40” Background=”Blue” Content=”Click Me” />
</Page>
//code can make all-等效的c#程序片段 Button blueButton = new Button(); blueButton.Width = 100; blueButton.Height = 40; blueButton.Content = “Click Me”; blueButton.Background = new SolidColorBrush(Colors.Blue);
xaml是微软描述wpf api的ui语言,是在普遍意义上将.net对象实例以直观形式进行序列化的语言(每个对象标签自动对应运行时的一个实例)它的“口号”是用一种简单形式写程序;
扩展名为.xaml;
可编译、可解释,编译后可嵌入c#例程代码(procedural code);
element对应object instance,attribute即properties of object;
根元素总是Page或Window标签;
控件 类库-System.Windows.Controls;
x:Name,对代码可访问的对象作唯一命名;
属性元素语法(property element syntax);
标签的Content属性可以是值类型,更可以是对象类型,系统自动进行类型转换;
<Button>
<Button.Content>
<Ellipse Width=”24” Height=”24” Fill=”Yellow” />
</Button.Content>
</Button>
标记扩展(markup extensions),xaml中的重要概念,注意它的名值对的值不能使用双引号;
常用标记扩展/扩展标记如下:
Name | Description |
Binding | The extension used for binding data. |
StaticResource | This is used for retrieving data from an application’s resources. Static resources are not expected to change while the application is running. |
DynamicResource | Similar to StaticResource, except that the data in the resource might change during runtime. |
x:Null | Used for specifying a null value in XAML. |
x:Type | This extension is used for supplying a System.Type object. |
X:Array | This allows you to define an array of objects in XAML. |