首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >3.x名称空间详解

3.x名称空间详解

作者头像
步步为营DotNet
发布2026-06-17 09:13:25
发布2026-06-17 09:13:25
160
举报

x名称空间内容

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

名称

在XAML中出现的形式

x:Array

标签扩展

x:Class

Attribute

x:ClassModifier

Attribute

x:Code

XAML指令元素

x:FieldModifier

Attribute

x:Key

Attribute

x:Name

Attribute

x:Null

标签扩展

x:Shared

Attribute

x:Static

标签扩展

x:SubClass

Attribute

x:Type

标签扩展

x:TypeArguments

Attribute

x:Uid

Attribute

x:XData

XAML指令元素

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集合。

代码语言:javascript
复制
<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>
代码语言:javascript
复制
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属性
代码语言:javascript
复制
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类
代码语言:javascript
复制
<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
代码语言:javascript
复制
<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中

代码语言:javascript
复制
<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>
另一种声明语法

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

代码语言:javascript
复制
<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"/>的形式增加内容,所以只能采用标签声明的语法:

代码语言:javascript
复制
<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成员一定是数据类型的属性或者字段

代码语言:javascript
复制
<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标签内。

代码语言:javascript
复制
<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>

books"> <x:XData> </x:XData> </Window.Resources>

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2026-06-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • x名称空间内容
  • x名称空间中的Attribute
    • x:Class
    • x:ClassModifier
    • x:Name
    • x:FieldModifier
    • x:Key
    • x:Shared
  • x名称空间中的标记扩展
    • x:Type
    • x:Null
    • 另一种声明语法
    • x:Array
    • x:Static
  • XAML指令元素
    • x:Code
    • x:Data
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档