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!=null)
- {
- 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
WPF学习之X名称空间详解的更多相关文章
- [No000013F]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系统表空间指的是数据库系统创建时需要的表空间,这些表空间在数据库创建时自动创建,是每个数 ...
随机推荐
- python中的静态成员方法和类成员方法
Python的静态方法和类成员方法都可以被类或实例访问,两者概念不容易理清,但还是有区别的: 1)静态方法无需传入self参数,类成员方法需传入代表本类的cls参数: 2)从第1条,静态方法是无法访问 ...
- @Value 和 @ConfigurationProperties 获取值的比较
1.不同点 (1)@ConfigurationProperties(prefix = "person") 功能:批量注入配置文件中的属性 SpEL:不支持表达式 JSR303数据校 ...
- line-height测量及使用
1.line-height定义 line-height表示行高,即两行文字基线间的距离. 以下是图示说明: 行高是2条红线之间的距离,即:1+2+3+4 在实际测量中,基线不好找,可测量顶线到顶线的距 ...
- Caffe源代码中Solver文件分析
Caffe源代码(caffe version commit: 09868ac , date: 2015.08.15)中有一些重要的头文件,这里介绍下include/caffe/solver.hpp文件 ...
- eclipse Java compiler level does not match the version of the installed Java project facet.
eclipse Java compiler level does not match the version of the installed Java project facet. Create ...
- oracle卸载清除注册表(彻底卸载)
用Oracle自带的卸载程序不能从根本上卸载Oracle,从而为下次的安装留下隐患,那么怎么才能完全卸载Oracle呢? 那就是直接注册表清除,步骤如下: . 开始->设置->控制面板-& ...
- 改变maven父子项目视图为树状
完毕
- java常见错误--Access restriction: The type BASE64Encoder
Access restriction: The type BASE64Encoder is not accessible due to restrict 在Eclipse中编写Java代码时,用到了B ...
- 腾讯云-搭建 JAVA 开发环境
搭建 JAVA 开发环境 搭建 JAVA 开发环境 任务时间:18min ~ 20min 此实验教大家如何配置 JDK .Tomcat 和 Mysql 00.安装 JDK JDK 是开发Java程序必 ...
- RCF库ClientStub.setAutoReconnect
这个选项为false时,当连接断开时,第一次调用服务会抛出异常,而第二次调用时,也会自动连接.