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>
<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>
XAML指令元素只有两个:
- x:Code
- x:XData

写博客真的是一件辛苦的事,写的我头晕眼花。

浙公网安备 33010602011771号