1.xmal元素介绍
新建一个wpf窗口后,会生成如下的xaml语句
<Window x:Class="ControlExercise.Window1"
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:ControlExercise"
mc:Ignorable="d"
Title="Window1" Height="450" Width="800">
<Grid>
</Grid>
</Window>
那好,我们就从最简单的,vs框架帮我们生成的代码开始说起。
xaml中最基本的语法元素就是标签、属性、内容。
- 标签:
通常以<>
开始,</>
结束,一个标签的声明通常标识一个对象。
两种写法:
1)非自闭合标签<Window></Window>, <Grid></Grid>
2)自闭合标签<Window />, <Grid />
-
属性:
通常以键值对形式出现,Title="MainWindow" Height="450" Width="800"
-
内容:
一组标签之间夹杂的文本或其他标签,都被称之为这个标签的内容。此处Window标签的内容就是一个<Grid></Grid>
标签。
Window x:Class="ControlExercise.Window1"
表示此xaml窗口对应的C#类,为ControlExercise命名空间下的Window1类。
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
表示引用wpf界面表现相关的命名空间,类似于我们C#类中的using。
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
表示引用xaml相关的命名空间。
2.修改wpf的启动窗口
在App.xaml中的Application标签里,有StartupUri属性,指示了当前程序最先启动的窗口,如果我们的程序有很多窗口,调试的时候比较方便,想启动谁就改成那个窗口
<Application x:Class="ControlExercise.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ControlExercise"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
3.为Grid标签设置背景颜色
元素的属性也可以通过标签来表示,也可以在Grid标签直接写BackGround,两种形式都表示设置Grid的Background属性值。
<Grid>
<Grid.Background>
<SolidColorBrush Color="Blue">
</SolidColorBrush>
</Grid.Background>
</Grid>
<Grid Background="Blue">
</Grid>
xaml中所有元素都是由属性,标签,内容构成的,只有先了解了这些基本概念后,才能方便以后的继续学习。