1.狂妄的WPF
相对传统的Windows图形编程,需要做很多复杂的工作,引用许多不同的API。例如:WinForm(带控件表单)、GDI+(2D图形)、DirectX API(3D图形)以及流媒体和流文档等,都需要不同的API来构建应用程序。
WPF就是看着上面的操作复杂和不爽,自己决定做老大,想用DirectX技术涵盖一切,于是想要将上述的东西全部融合到自身,减少复杂度,让编程变得爽起来的技术。
而不可否认的是,WPF虽然很狂妄,但是这种技术里面还是有不少的可圈可点的东西。而支持WPF狂妄的资本,则就是和它后台代码可以前后分离的XAML技术。下面用30分钟时间说一下XAML。
2.什么是XAML
一个界面程序的核心,无疑就是界面和后台代码,而xaml就是微软为构建应用程序界面而创建的一种描述性语言,也就是说,这东西是搞界面的。
先上一段xaml代码:
<Window x:Class='MyXaml.Window1'
xmlns='http://www.easck.com/winfx/2006/xaml/presentation'
xmlns:x='http://www.easck.com/winfx/2006/xaml'
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height='30'/>
<RowDefinition Height='30'/>
<RowDefinition Height='30'/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width='Auto'/>
<ColumnDefinition Width='*'/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column='0' Grid.Row='0' FontWeight='Bold' Text='姓名:' Width='30'/>
<TextBlock Grid.Column='0' Grid.Row='1' FontWeight='Bold' Width='30'>性别:</TextBlock>
<TextBlock Grid.Column='0' Grid.Row='2' FontWeight='Bold' Width='30' Text='年龄'></TextBlock>
<TextBox Grid.Column='1' Grid.Row='0' FontWeight='Bold' Width='100' />
<TextBox Grid.Column='1' Grid.Row='1' FontWeight='Bold' Width='100'/>
<TextBox Grid.Column='1' Grid.Row='2' FontWeight='Bold' Width='100'/>
</Grid>
</Window>
上述xaml是我设计了一个三行两列的界面,运行之后显示如下:

在此,我没有写一行c#代码,但是它竟然可以运行,所以也可以说它也是一种编程语言。只不过它更关注界面上面的东西而已。
那么它的运行是如何产生的?下面看几个东西:
x:Class='MyXaml.Window1' ——利用class特性指定c#类名(后台c#代码)
xmlns:x=http://www.easck.com/winfx/2006/xaml——这表示利用x代替XAML的命名空间。用于包含特定的关键字和System.Windows.Markup中类型的子集。










