3.x名称空间详解

3.x名称空间详解

x名称空间内容

x命名空间映射的是解析XAML语言相关的类库,XAML语言也需要自己的编译器,最终形成中间语言存储在程序集中。对XAML编译器进行控制的命令就放在x命名空间下。

名称在XAML中出现的形式
x:Array标签扩展
x:ClassAttribute
x:ClassModifierAttribute
x:CodeXAML指令元素
x:FieldModifierAttribute
x:KeyAttribute
x:NameAttribute
x:Null标签扩展
x:SharedAttribute
x:Static标签扩展
x:SubClassAttribute
x:Type标签扩展
x:TypeArgumentsAttribute
x:UidAttribute
x:XDataXAML指令元素

x名称空间中的Attribute

x:Class

告诉XAML编译器,将XAML标签编译结果与后台代码中指定的类合并

  • 只能用于根节点
  • 根节点的类型要与x:Class的值所指示的类型保持一致
  • x:Class的值的类型必须为partial

x:ClassModifier

生成的类具有怎样的访问级别

  • 标签必须有x:Class
  • x:ClassModifier的值必须与x:Class所指示类的访问级别一致
  • x:ClassModifier的值参见TypeAttributes,注意,首字母要小写

x:Name

为标签声明Name属性以创建引用变量在C#代码中访问

除了x:Name的Attribute,还有Name的Attribute,Name是定义在FrameworkElement类中,该类是WPF的基类。当一个元素如Button有Name属性时,两者效果相同。对于没有Name属性的元素,为了在XAML声明时也创建引用变量以在C#代码中访问,则只能使用x:Name,因为x:Name涵盖了所有Name的所有功能,所以推荐全部使用x:Name。

x:FieldModifier

对使用了x:Name而创建了的引用变量设定访问级别

使用了x:Name默认的访问级别是internal

  • 必须同时结合x:Name来使用

x:Key

为资源贴上用于检索的标签

几乎所有元素都有Resource属性,这个属性是key-value集合。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<Window x:Class="WpfApp3.MainWindow"
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:WpfApp3"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<sys:String x:Key="myString">hello world</sys:String>
</Window.Resources>
<StackPanel>
<TextBox Text="{StaticResource ResourceKey=myString}" />
<TextBox x:Name="txtBox" />
<Button Content="Show" Click="Button_Click" Width="60"/>
</StackPanel>
</Window>
1
2
3
4
5
private void Button_Click(object sender, RoutedEventArgs e)
{
string str = this.FindResource("myString") as string;
this.txtBox.Text = str;
}

x:Shared

当时用资源时,检索同一对象,如何确定是同一对象还是对象的副本?默认x:Shared=“true”

  • x:Shared一定要和x:Key配合使用

x名称空间中的标记扩展

x:Type

x:Type的值是一个数据类型

案例

  1. 新增MyButton类型,其中有UserWindowType属性
1
2
3
4
5
6
7
8
9
10
class MyButton : Button
{
public Type UserWindowType { set; get; }
protected override void OnClick()
{
base.OnClick();
Window win = Activator.CreateInstance(this.UserWindowType) as Window;
win ?.ShowDialog();
}
}
  1. 新增一个window类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<Window x:Class="WpfApp3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Window1" Height="450" Width="400" >
<StackPanel Background="Blue">
<TextBox/>
<TextBox/>
<TextBox/>
<Button x:Name="btnMyButton" Content="Show" Width="60" Height="50"/>
</StackPanel>
</Window>
  1. 将新增的window类作为数据类型赋给MyButton的UserWindowType
1
2
3
4
5
6
7
8
9
10
11
12
13
<Window x:Class="WpfApp3.MainWindow"
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:WpfApp3"
Title="MainWindow" Height="450" Width="400">
<Window.Resources>
<sys:String x:Key="myString">hello world</sys:String>
</Window.Resources>
<StackPanel>
<local:MyButton Content="show" UserWindowType="{x:Type TypeName=local:Window1}"/>
</StackPanel>
</Window>

x:Null

当一个属性具有默认值,而又不需要默认值得时候使用,常常用在Style中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<Window x:Class="WpfApp3.MainWindow"
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:WpfApp3"
Title="MainWindow" Height="450" Width="400">
<Window.Resources>
<!--将x:key和TargetType都设置成Button属性,这样所有的Button默认使用该样式-->
<Style x:Key="{x:Type Button}" TargetType="{x:Type Button}">
<Setter Property="Width" Value="60"/>
<Setter Property="Height" Value="36"/>
<Setter Property="Margin" Value="5"/>
</Style>
</Window.Resources>
<StackPanel>
<Button Content="ok"/>
<Button Content="ok"/>
<Button Content="ok"/>
<Button Content="ok" Style="{x:Null}"/>
</StackPanel>

</Window>

另一种声明语法

标记扩展也可以使用属性元素的方式赋值

1
2
3
4
5
6
7
<Button Content="ok" Style="{x:Null}"/>   可以写成

<Button Content="ok">
<Button.Style>
<x:Null/>
</Button.Style>
</Button>

x:Array

x:Array的作用是通过它的Items属性向外暴露一个ArrayList实例,ArrayList内成员的类型由x:Array的Type指明。

<ListBox ItemsSource="{x:Array Type=sys:String}"/>但是此时不能使用<ListBox ItemsSource="{x:Array Type=sys:String} Items=XXXXXXX"/>的形式增加内容,所以只能采用标签声明的语法:

1
2
3
4
5
6
7
8
9
<ListBox>
<ListBox.ItemsSource>
<x:Array Type="sys:String">
<sys:String>aaa</sys:String>
<sys:String>bbb</sys:String>
<sys:String>ccc</sys:String>
</x:Array>
</ListBox.ItemsSource>
</ListBox>

x:Static

在XAML文档中使用static成员,因为XAML不能编写逻辑代码,所以x:Static访问的static成员一定是数据类型的属性或者字段

1
2
3
4
5
6
7
8
9
10
11
12
<Window x:Class="WpfApp3.MainWindow"
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:WpfApp3"
Title="{x:Static local:MainWindow.WindowTitle}" Height="450" Width="400">

<StackPanel>
<TextBlock Text="{x:Static local:MainWindow.ShowText}"/>
</StackPanel>

</Window>

XAML指令元素

x:Code

作用是在XAML中包含C#代码,不推荐

x:Data

WPF中包含数据的对象成为数据源,用于把数据源中的数据提供给使用者的对象为数据提供者。XmlDataProvider,专门用于提供XML化的数据,如果在XAML里声明一个带有数据的XmlDataProvider实例,就要把XmlDataProvider实例数据放在x:Data标签内。

1
2
3
4
5
6
7
8
9
10
11
<Window.Resources>
<XmlDataProvider x:Key="InventoryData" XPath="Inventory/books">
<x:XData>
<Fruits>
<Fruits Name="peach"/>
<Fruits Name="apple"/>
<Fruits Name="orange"/>
</Fruits>
</x:XData>
</XmlDataProvider>
</Window.Resources>
作者

步步为营

发布于

2024-05-08

更新于

2025-03-15

许可协议