当前位置:首页 > Windows程序 > 正文

WPF01(xaml)

2021-03-26 Windows程序

<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"> <Grid> </Grid> </Window>

然后我们来对比一下webform中的page默认生成页面

1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 <html xmlns="http://www.w3.org/1999/xhtml"> 5 <head runat="server"> 6 <title></title> 7 </head> 8 <body> 9 <form runat="server"> 10 <div> 11 </div> 12 </form> 13 </body> 14 </html>

我们发现xaml很像xml结构,是的,它是一种遵循xml规范的界面描述语言。

一:xaml简述

首先我默认大家都是熟悉webform的开发者。

1:x:Class

这个标记在webform中有对应的标记吗?有的,也就是这里的CodeBehind。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

2:xmlns

这个在webform中也有对应标记吗?首先我们要知道xmlns是干嘛的?哦,导入命名空间用的,那我们马上就想到webform中的对应标记import。

<%@ Import Namespace="System.IO" %>

那么下一个问题就是两者有什么不同吗?我们知道webform中导入命名空间需要一个一个的导入,4个命名空间就要写4个import,而xaml可以做到多

个命名空间做为一个导入。

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

其实也就导入了如下4个wpf开发必备的dll,这个命名空间也是xaml中默认的命名空间。

3:xmlns:x

如果我们需要导入一些自定义的命名空间,那么我们就需要加上用“: + 自定义名称”的形式,这里微软导入了一个自定义的命名空间。

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

下面我们也来导入一个命名空间,实际开发中我们当然我们不会做成url的形式,这里我就取名为sys前缀

<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System.IO;assembly=mscorlib" Title="MainWindow"> <Grid> </Grid> </Window>

4:Grid

我们都知道在n年前的html网页布局中都采用table的形式,table嵌套table,table跨行跨列等手段构建了漂亮的网页,这种排版思路也应用到了wpf中。

<1>:划分位置

我们画个两行两列的界面布局,这里我们只要将”鼠标“放在”红色方框“中,就会出现小三角,我们点击就可生成一个分割线,红色小圈圈标记着“行列”

的分割比列。

然后我们看一下生成的代码

<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System.IO;assembly=mscorlib" Title="MainWindow"> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> </Grid> </Window>

我们奇怪的发现为什么宽度有“*”号,这里要解释一下wpf中对“宽高度”的设置有三个形式。

①:绝对尺寸         <RowDefinition />

②:自动尺寸         <RowDefinition />

③:按比例尺寸      <RowDefinition />

那我们就有疑问了,到底161* 是多少呢?计算规则如下:

我们这里的窗体Height=350。

第一行高度为: 350/(161+150)*161  

第二行高度为:350(161+150)*150

<2>: UI元素布局

①:UI元素对号入座

很简单,我们只要在button的Grid属性上设置button应该放在哪一个单元格即可。

②:UI元素跨行跨列

二:xaml中扩展标记

扩展标记分为两种:wpf级别和xaml级别。

<1> wpf级别扩展标记

①: StaticResource

用于获取资源的值,值获取在xaml编译的时候完成,什么意思呢?先举个例子。

首先,我们发现有一个window.Resources,这东西我们可以认为是在MainWindow类中定义的全局变量,这里我就定义个name=“一线码农”的

变量,那么textblock获取变量的方式就可以通过StaticResource。

②:DynamicResource

跟StaticResource唯一不同的是,它是在运行时获取的,如果大家知道C#里面的dynamic关键字,我想就不用解释了,上代码。

③:Binding

还是在webform中找一下关键字吧,相当于webform中的Eval,上代码说话。

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/67668.html