[No000013F]WPF学习之X名称空间详解
X名称空间里面的成员(如X:Name,X:Class)都是写给XAML编译器看的、用来引导XAML代码将XAML代码编译为CLR代码。
4.1X名称空间里面到底都有些什么?
x名称空间映射的是:http://schemas.microsoft.com/winfx/2006/xaml,望文生义,它包含的类均与解析XAML语言相关,所以亦称之为“XAML名称空间”。
与C#语言一样,XAML也有自己的编译器。XAML语言被解析并编译,最终形成微软中间语言保存在程序集中。在解析和编译XAML的过程中,我们经常要告诉编译器一些重要的信息,如XAML编译的结果应该和哪个C#代码编译的结果合并、使用XAML声明的元素是public还是private访问级别等等。这些让程序员能够与XAML编译器沟通的工具就存在X:名称空间中。


我们注意到,它分为Attribute、标签扩展、XAML指令元素三个种类。下面我们讲讲它们的具体用法:
4.2 X名称空间中的Attribute
前面我们已经讲过,Attribute和Property是两个层面上的东西,Attribute是语言层面上的东西,是给编译器看的,Property是面向对象层面上的东西,是给编程逻辑看。而且一个标签中的Attribute大部分对应对象的Property。在使用XAML编程的时候,如果你想给它加一点特殊的标记来改变XAML对它的解析,这时候就需要额外的给它添加一些Attribute了。比如,你想告诉XAML编译器将哪个编译结果和那个C#编译的类合并,这时候就必须为这个标签添加X:Class Attribute来告诉编译器。X:Class并不是对象成员,而是重X空间硬贴上去的。让我们浏览一下常用的Attribute。
4.2.1 x:Class
这个Attribute是告诉XAML编译器将XAML编译器编译的结果和后台编译结果的哪一个类进行合并,使用x:Class有以下几点要求:
- 这个Attribute只能用于根节点。
- 使用x:Class的根节点的类型要与x:Class的值所指示的一致。
- x:Class的值所指示的类型在声明的时候必须使用partial关键字。
- x:Class已经在剖析最简单的XAML的时候已经讲过,在这就不多讲了。
4.2.2 X:ClassModiffier
这段代码是告诉XAML编译器有标签编译成的类具有什么样的访问级别。
使用这个Attribute的时候需要注意的是:
- 标签必须具有x:Class Attribute。
- X:ClassModiffier的值必须与X:Class所指定类的访问权限一致。
- X:ClassModiffier的值随后台代码编译语言的不同而有所不同。
- <Window x:Class="WpfApplication2.Window5"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="Window5" Height="300" Width="300">
- <Grid>
- <StackPanel Height="218" HorizontalAlignment="Left" Margin="19,31,0,0" VerticalAlignment="Top" Width="237">
- <TextBox Height="23" Width="120" />
- <Button Content="Button" Height="23" Width="75" />
- </StackPanel>
- </Grid>
- </Window>
这篇代码中通篇没有出现一次名字,但是我们可以通过引用者的层级关系来找到我们最终想要的控件,我们在Button的Click下写如下代码:
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- StackPanel panel = this.Content as StackPanel;
- TextBox textBox = panel.Children[0] as TextBox;
- if (!string.IsNullOrEmpty(textBox.Name))
- {
- textBox.Text = textBox.Text;
- }
- else
- {
- textBox.Text = "NULL";
- }
- }
this.Content引用着StackPanel的实例,StackPanel.Children[0]又引用着TextBox的实例。知道了这个关系,就可以一路顺着查找下来并同时进行类型转换,最终TextBox中显示的值是NULL。
- <StackPanel Height="218" HorizontalAlignment="Left" Margin="19,31,0,0" VerticalAlignment="Top" Width="237">
- <TextBox Height="23" Width="120" x:Name="txtName" x:FieldModifier="internal"/>
- <Button Content="Button" Height="23" Width="75" Click="Button_Click" x:Name="btntest" x:FieldModifier="public"/>
- </StackPanel>
因为x:FidleModifier是应用变量的访问级别,所以要配合x:Name一起使用。否则没有引用变量,何来引用变量访问级别。
- <Window x:Class="WpfApplication2.Window4"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:sys="clr-namespace:System;assembly=mscorlib"
- xmlns:local="clr-namespace:WpfApplication2"
- Title="Window4" Height="369" Width="675">
- <Window.Resources>
- <local:Human x:Key="human" Child="ABC"></local:Human>
- <sys:String x:Key="myString">测试</sys:String>
- <Style x:Key="{x:Type Button}" TargetType="{x:Type Button}">
- <Setter Property="Width" Value="30"></Setter>
- <Setter Property="Background" Value="black"></Setter>
- </Style>
- </Window.Resources>
- <Grid>
- <Label Content="{ StaticResource ResourceKey=myString}" Height="28" HorizontalAlignment="Left" Margin="177,81,0,0" Name="label1" VerticalAlignment="Top" />
- </Grid>
- </Window>
资源不但可以在XAML中使用,也可以在C#中访问,C#中使用如下方式:
- string str = this.FindResource("myString") as string;
- this.label1.Content = str;
4.2.6 x:Shared
- <UserControl x:Class="WpfApplication2.UserControl1"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- mc:Ignorable="d"
- d:DesignHeight="52" d:DesignWidth="128">
- <Grid>
- <Button Content="Button" Height="30" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="106" Click="button1_Click" />
- </Grid>
- </UserControl>
- /// <summary>
- /// UserControl1.xaml 的交互逻辑
- /// </summary>
- public partial class UserControl1 : UserControl
- {
- public UserControl1()
- {
- InitializeComponent();
- }
- public Type MyWindowType { get; set; }
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- Window myWin = Activator.CreateInstance(this.MyWindowType) as Window;
- if(myWin!=)
- {
- myWin.Show();
- }
- }
- }
- <Window x:Class="WpfApplication2.Window4"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:sys="clr-namespace:System;assembly=mscorlib"
- xmlns:local="clr-namespace:WpfApplication2"
- Title="Window4" Height="369" Width="675">
- <Window.Resources>
- <local:Human x:Key="human" Child="ABC"></local:Human>
- <sys:String x:Key="myString">测试</sys:String>
- <Style x:Key="{x:Type Button}" TargetType="{x:Type Button}">
- <Setter Property="Width" Value="30"></Setter>
- <Setter Property="Background" Value="black"></Setter>
- </Style>
- </Window.Resources>
- <Grid>
- <local:UserControl1 HorizontalAlignment="Left" Margin="292,244,0,0" x:Name="userControl11" VerticalAlignment="Top" MyWindowType="{x:Type TypeName=local:Window1}"/>
- </Grid>
- </Window>
回顾一下之前的标记扩展语法,因为TypeExtension类的构造器可以接受数据类型名做为参数,所以我们完全可以这样写:
- UserWindowType="{x:Type local:Window1}"
编译并运行程序,单击主窗体上的按钮,自定义窗口就会显示出来,我们还可以多写几个窗体来扩展这个程序,到时后只需要修改MyWindowType里面的值就可以了。
- <Window x:Class="WpfApplication2.Window4"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:sys="clr-namespace:System;assembly=mscorlib"
- xmlns:local="clr-namespace:WpfApplication2"
- Title="Window4" Height="369" Width="675">
- <Window.Resources>
- <Style x:Key="{x:Type Button}" TargetType="{x:Type Button}">
- <Setter Property="Width" Value="30"></Setter>
- <Setter Property="Background" Value="black"></Setter>
- </Style>
- </Window.Resources>
- <Grid>
- <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="180,256,0,0" Name="button1" VerticalAlignment="Top" Click="button1_Click" />
- <Label Content="{ StaticResource ResourceKey=myString}" Height="28" HorizontalAlignment="Left" Margin="177,81,0,0" Name="label1" VerticalAlignment="Top" />
- <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button2" VerticalAlignment="Top" />
- <Button Content="{x:Static local:Window4.Test}" Height="23" HorizontalAlignment="Left" Margin="128,12,0,0" Name="button3" VerticalAlignment="Top" Style="{x:Null}"/>
- </Grid>
- </Window>
当然了,x:null也可以使用属性标签来设置这个值,前面已经讲过,在这就不在讲了。
- <Window x:Class="WpfApplication2.Window4"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:sys="clr-namespace:System;assembly=mscorlib"
- xmlns:local="clr-namespace:WpfApplication2"
- Title="Window4" Height="369" Width="675">
- <Grid>
- <ListBox Height="100" HorizontalAlignment="Left" Margin="435,110,0,0" Name="listBox1" VerticalAlignment="Top" Width="176">
- <ListBox.ItemsSource>
- <x:Array Type="sys:String">
- <sys:String>Jim</sys:String>
- <sys:String>Darren</sys:String>
- <sys:String>Frank</sys:String>
- </x:Array>
- </ListBox.ItemsSource>
- </ListBox>
- </Grid>
- </Window>
4.3.4 x:Static
- public Window4()
- {
- InitializeComponent();
- //SolidColorBrush brush = new SolidColorBrush();
- //brush.Color = Colors.Blue;
- //this.rectangle1.Fill = brush;
- }
- public static string Test = "明月松间照,清泉石上流。";
- <Window x:Class="WpfApplication2.Window4"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:sys="clr-namespace:System;assembly=mscorlib"
- xmlns:local="clr-namespace:WpfApplication2"
- Title="Window4" Height="369" Width="675">
- <Grid>
- <Button Content="{x:Static local:Window4.Test}" Height="23" HorizontalAlignment="Left" Margin="128,12,0,0" Name="button3" VerticalAlignment="Top" Style="{x:Null}"/>
- </Grid>
- </Window>
如果一个程序需要支持国际化,一般需要把显示的字符串保存在一个资源类的Static属性中,所以支持国际化的程序UI中对x:Static的使用相当的频繁。
XAML指令元素只有两个:
- x:Code
- x:XData

[No000013F]WPF学习之X名称空间详解的更多相关文章
- WPF学习之X名称空间详解
X名称空间里面的成员(如X:Name,X:Class)都是写给XAML编译器看的.用来引导XAML代码将XAML代码编译为CLR代码. 4.1X名称空间里面到底都有些什么? x名称空间映射的是:htt ...
- WPF读书笔记 x名称空间详解(第二天)
每天看一点,每天进步一点. x名称空间映射的是http://schemas.microsoft.com/winfx/2006/xaml,它包含的类均与解析XAML语言关,亦可称为"XAML名 ...
- 深入浅出WPF-04.x名称空间详解
x名称空间详解 几个需要特别说明的名称空间: x:Class 用来标记XAML和后台代码之间的合并关系.x:Class根节点的类型必须和x:Class值指向的类型保持一致.x:Class的值指向的类型 ...
- 9、XAML名称空间详解
XAML命名空间 <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ...
- WPF 基础 - x 名称空间详解
名称 种类(默认Attribute) 备注 x:Array 标记拓展 可作为 ListBox.ItemsSource 的值 x:Class 指定与 .cs 中哪个类合并,所指示的类型在声明时使用 pa ...
- 重新学习MySQL数据库7:详解MyIsam与InnoDB引擎的锁实现
重新学习Mysql数据库7:详解MyIsam与InnoDB引擎的锁实现 说到锁机制之前,先来看看Mysql的存储引擎,毕竟不同的引擎的锁机制也随着不同. 三类常见引擎: MyIsam :不支持事务,不 ...
- 《从0到1学习Flink》—— Flink 配置文件详解
前面文章我们已经知道 Flink 是什么东西了,安装好 Flink 后,我们再来看下安装路径下的配置文件吧. 安装目录下主要有 flink-conf.yaml 配置.日志的配置文件.zk 配置.Fli ...
- Java学习-007-Log4J 日志记录配置文件详解及实例源代码
此文主要讲述在初学 Java 时,常用的 Log4J 日志记录配置文件详解及实例源代码整理.希望能对初学 Java 编程的亲们有所帮助.若有不足之处,敬请大神指正,不胜感激!源代码测试通过日期为:20 ...
- Oracle表空间详解
Oracle表空间详解 1.表空间的分类 Oracle数据库把表空间分为两类:系统表空间和非系统表空间. 1.1系统表空间指的是数据库系统创建时需要的表空间,这些表空间在数据库创建时自动创建,是每个数 ...
随机推荐
- 基于CentOS 搭建 Seafile 专属网盘
系统要求:CentOS 7.2 64 位操作系统 安装 Seafile 安装依赖环境 使用 yum 安装 Python 及 MySQL: yum install python python-setup ...
- spring-mybatis-data-common程序级分表操作实例
spring-mybatis-data-common-2.0新增分表机制,在1.0基础上做了部分调整. 基于机架展示分库应用数据库分表实力创建 create table tb_example_1( i ...
- oracle登陆提示“ora-01031 insufficient privileges”
本机装了服务端的oracle11.2g,一直没用,中间换过系统的登陆用户.今早发现登陆数据库时发现老提示“ora-01031 insufficient privileges”,以为监听没配置好,试过了 ...
- php验证码--图片
这里我们介绍图片验证码的制作,有关字符验证码能够參考下面文章: 点击打开链接 图片验证码的制作分三步: 1.制作图片库 2.随机选取一张图片 3.输出图片内容 代码例如以下(这里为了方便我直接用的本地 ...
- SNF快速开发平台MVC-名片管理(实际名片样式)
名片管理实际的做的意义在于演示应用,在这里使用的技术有排序控件,查询条件.自由样式瀑布流式分页等技术. 下面是自由样式效果图: 下面表格样式效果图: 具体操作: 新增名片 在新增时可以上传图像进行裁剪 ...
- 【iCore4 双核心板_FPGA】例程十三:基于SPI的ARM与FPGA通信实验
实验现象: 1.先烧写ARM程序,然后烧写FPGA程序. 2.打开串口精灵,通过串口精灵给ARM发送数据从而给FPGA发送数据 ,会接收到字符HELLO. 3.通过串口精灵发送命令可以控制ARM·LE ...
- 看雪CTF第十五题
1.直接运行起来,再用OD附加 在此处luajit加载并调用main函数 004021C7 E8 64FE0000 call CrackMe. ; luaL_newstate 004021CC 8BF ...
- WebMisSharp,WebMisCentral,企业框架正则表达式规则共享专版
ElegantWM.WebUI/Application/common/src/Tools.js 欢迎大家贡献更多的正则验证规则,目前支持如下: /*************************** ...
- org.apache.http.TruncatedChunkException: Truncated chunk ( expected size: 47956; actual size: 35656)
在使用httpcomponents-client-4.2.1时,任务运行一段时间就抛出以下一场 下面是异常的堆栈信息: org.apache.http.TruncatedChunkException: ...
- Java如何获取URL连接的日期?
Java编程中,如何获取URL连接的日期? 以下示例演示如何使用HttpURLConnection类的httpCon.getDate()方法获取URL连接的日期. package com.yiibai ...