首先,新建一个WPF应用程序:
可以看到.xmal文件中的内容为:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
</Grid>
</Window>
可以看到xmal的结构就是一种树形结构,有根节点、分支节点、叶子节点等等组成。
可以看到目前的最基本形式是根节点<Window> 下嵌套着<Grid>:
<Window>
<Grid>
</Grid>
</Window>
xmal是一种声明性语言,每个标签即代表了一个对象,各个对象之间要么是并列、要么是嵌套。而标签可以是上面<Grid>那种空标签也可以是<Window> 那种包含了各种特征的非空标签。
其中,标签的特征可以分为:Attribute、Property两类。
Property表示标签的属性,一般用来表示对象状态的量就被抽象为Property;
Attribute表示标签的特征,一般只与语言层面的东西相关。
可以看到上面的<Window>标签下:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
Title、Height、Width这些就是Window对象的Property;
前面的
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
则是在声明命名空间(xmlns即xml namespace);
xmlns特征的语法格式为:
xmlns[:可选的映射前缀]="命名空间"
最上面的:
x:Class="WpfApplication1.MainWindow"
是在使用名为Class的Attribute。
然后增加一些控件内容:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="279.6" Width="491.4">
<StackPanel Background="LightBlue">
<TextBox x:Name="textBox" Height="50" Margin="4" Background="LightCoral"/>
<TextBox x:Name="tex2Box" Height="50" Margin="4" Background="AliceBlue"/>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="textBox1" Width="150" Height="50" Margin="5" Background="LightGreen"/>
<TextBox x:Name="textBox2" Width="150" Height="50" Margin="5" Background="Beige"/>
<TextBox x:Name="textBox3" Width="150" Height="50" Margin="5" Background="LightPink"/>
</StackPanel>
<Button x:Name="button1" Margin="5" Width="200" Height="50">
<TextBlock Text="Button"/>
</Button>
</StackPanel>
</Window>
窗口效果如图: