深入浅出WPF1

目录结构

  • Properties:程序要用到的一些资源和配置信息。
  • Reference:标记了当前项目需要引用哪些其他项目。
  • App.xaml:程序主体。
  • Window1.xaml:程序主窗体。

XAML

XAML由XML派生而来,所以很多XML中的概念在XAML中通用。

xmlns

xmlns—-XML-Namespace定义命名空间。

1
2
3
4
5
6
7
8
9
10
11
12
<Window x:Class="深入浅出WPF.MainWindow"   //使用命名空间x中的class
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" //默认命名空间,UI相关程序集
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" //映射前缀为x,的命名空间,语言解析处理相关程序集
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:深入浅出WPF"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>

</Grid>
</Window>

C#中的partial关键字

这个关键字可以把一个类分拆到多处定义,这样在XAML中解析生成的类就可以和C#中的部分合二为一了。partial机制可以把类的逻辑代码留在.cs中,使用C#实现,把UI分离除去。

WPF的树形结构

WPF的框架是树状的,以为节点,一层一层向下包含。
这种树形结构对整个WPF都有非常重要的意义。

XAML对象和属性

1
2
3
4
5
6
7
8
9
10
11
12
13
<Window x:Class="深入浅出WPF.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:深入浅出WPF"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Rectangle x:Name="rectangle" Width="200" Height="120" Fill="Blue"/> //声明一个Rctangle对象,并设置属性

</Grid>
</Window>

x命名空间

x:class

这个Attribute的作用是告诉XAML编译器将XAML标签的编译结果与后台代码指定的类合并。

x:ClassModifier

类具有怎样的访问控制级别。

x:Name

为这个控件对象创建引用变量,并将对象的Name属性也设置为x:Name的值。

x:FieldModifier

修改引用变量访问级别

x:Key

通过Key检索资源,可以把重复利用资源

x:Shared

通过x:Key检索资源,如果x:Shared为true则检索的是同一对象,为false则为副本。默认为true。

-------------本文结束感谢您的阅读-------------