引用地址:https://blog.csdn.net/niechaoya/article/details/104191715
字母x其实是XML的首字母,x命名空间对应的就是这一条语句:xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml",它包含的类均与解析XAML语言相关,主要分为3类:Attribute、标记扩展、指令元素
1、Attribute
1.1、x:Class
这个Attribute的作用是告诉XAML编译器将XAML标签的编译结果与后台代码中指定的类合并,这个在我的上一篇文章(XAML入门)中有详细的讲解
1.2、x:ClassModifier
这个Attribute的作用是告诉XAML编译器由标签编译生成的类具有怎么的访问控制级别。
注意:XAML中类的访问控制级别必须和后台的类一样,因为这两个类其实是一个类,只不过是用partial关键字修饰了。
1.3、x:Name
这个x:Name的作用是给XAML标签定义一个引用变量,其含义和C#定义引用变量是一样的。
那x:Name和Name属性有什么区别呢?答案是没有区别。但是如果对于没有Name属性的标签,就只能使用x:Name了。x:Name的使用范围比Name要大,所以一般情况下我们推荐使用x:Name
1.4、x:FieldModifier
x:ClassModifier是给类定义访问控制级别,看名字就知道x:FieldModifier是给字段定义访问控制级别的。如:
<TextBox Text="你好" x:Name="txt1" x:FieldModifier="public"/>
1.5、x:Key
x:Key的作用就是为资源贴上用于检索的索引,检索的方式是key-value。这个资源指的是在XAML里面的<Window.Resources>里定义的。
举个例子:
<Window x:Class="WpfApp4.xKey"
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"
xmlns:local="clr-namespace:WpfApp4"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="xKey" Height="450" Width="800">
<Window.Resources>
<sys:String x:Key="myString">myKey</sys:String>
</Window.Resources>
<StackPanel>
<TextBox Text="{StaticResource ResourceKey=myString}"/>
</StackPanel>
</Window>
资源不但可以在XAML访问,也可以在C#后台代码访问,代码如下:
var str = this.FindResource("myString").ToString();
MessageBox.Show(str);
1.6、x:Shared
share翻译成中文是分享的意思,那这个Attribute的作用就是告诉XAML编译器每次检索到的资源是同一个对象还是对象的副本,和static关键字很像。它必须和x:key配合使用,默认情况下x:Shared=true。
2、扩展标记
2.1、x:Type
Type在编程语言层面上就是数据类型的意思,那x:Type就是表示数据类型本身。这样说可能很难理解,下面我们看个例子:
我们新建一个Button类的子类:
public class MyButton:Button
{
public Type UserWindowType { get; set; }
protected override void OnClick()
{
base.OnClick();
Window win = Activator.CreateInstance(UserWindowType) as Window;
if(win!=null)
{
win.ShowDialog();
}
}
}
新建一个myWindow窗体:
<Window x:Class="WpfApp4.MyWindow"
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"
xmlns:local="clr-namespace:WpfApp4"
mc:Ignorable="d"
Title="MyWindow" Height="600" Width="800">
<StackPanel>
<Button Content="OK"/>
</StackPanel>
</Window>
主窗体XAML代码:
<Window x:Class="WpfApp4.MainWindow"
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"
xmlns:local="clr-namespace:WpfApp4"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<local:MyButton Content="Show" UserWindowType="{x:Type local:MyWindow}"/>
</StackPanel>
</Window>
<local:MyButton Content="Show" UserWindowType="{x:Type local:MyWindow}"/>,这条语句首先使用了MyButton类,然后给属性UserWindowType赋值了,UserWindowType=MyWindow,因为这个属性的数据类型是Type,所以把一个窗体类本身赋值给这个属性没有什么问题。单机Show按钮,调用后台的定义的OnClick函数。我们来看一下这个函数:
protected override void OnClick()
{
base.OnClick();
Window win = Activator.CreateInstance(UserWindowType) as Window;
if(win!=null)
{
win.ShowDialog();
}
}
因为UserWindowType在主界面的XAML代码里被赋值为了MyWindow,所以调用Activator.CreateInstance会创建一个MyWindow的实例,因为MyWindow是继承自Window,所以单击按钮会显示MyWindow窗体。
2.2、x:Null
Null表示的是空值,大多数时候我们不用显示地为一个属性赋Null值,但如果一个属性具有默认值而我们又不需要这个默认值时就需要显示地设置Null值了。下面我们看个例子:
<Window x:Class="WpfApp4.MyWindow"
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"
xmlns:local="clr-namespace:WpfApp4"
mc:Ignorable="d"
Title="MyWindow" Height="600" Width="800">
<Window.Resources>
<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" Style="{x:Null}"/>
<Button Content="OK">
<Button.Style>
<x:Null/>
</Button.Style>
</Button>
</StackPanel>
</Window>
我们在资源里给Button按钮定义了一个Style,如果不设置Button的style为Null,那么默认Style的所有Button的Style都是在资源里面设置的Style。代码运行效果如下:
第三个按钮使用另一种方法给Style赋值,效果和第二个按钮一样,只是写法很繁琐。
2.3、x:Array
它的作用是通过它的Items属性向使用者暴露一个类型已知的ArrayList实例,ArrayList内成员的类型由x:Array的Type指定。下面我们看个例子:
<Window x:Class="WpfApp4.xArrayTest"
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"
xmlns:local="clr-namespace:WpfApp4"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="xArrayTest" Height="450" Width="800">
<Grid>
<ListBox Margin="5">
<ListBox.ItemsSource>
<x:Array Type="sys:String">
<sys:String>tim</sys:String>
<sys:String>tom</sys:String>
<sys:String>victor</sys:String>
</x:Array>
</ListBox.ItemsSource>
</ListBox>
</Grid>
</Window>
这里x:Array的Type是sys:String,也就是字符串。如果要使用字符串,还要引用System命名空间:xmlns:sys="clr-namespace:System;assembly=mscorlib",相当于C#里的:using System;
2.4、x:Static
它的功能是在XAML文档中使用数据类型的static成员,下面看一个例子:
XAML代码:
<Window x:Class="WpfApp4.xStaticTest"
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"
xmlns:local="clr-namespace:WpfApp4"
mc:Ignorable="d"
Title="{x:Static local:xStaticTest.WindowTitle}" Height="450" Width="800">
<StackPanel>
<TextBlock FontSize="32" Text="{x:Static local:xStaticTest.ShowText}"/>
</StackPanel>
</Window>
后台代码:
public static string WindowTitle = "山高月小";
public static string ShowText
{
get { return "水落石出"; }
}
运行效果:
这个应该很好理解,我就不做过多的解释了。
2.5、XAML指令元素
x:Code和x:XData。它的作用就是可以包含一些本应放置在后台代码的逻辑代码。这样做的好处是不用把XAML代码和C#代码分开放在2个文件中,但若不是遇到某些极端情况,这种方式不非常不建议采用的。因为这样做是违背了WPF的初衷的。
————————————————
版权声明:本文为CSDN博主「begeneral」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/niechaoya/article/details/104191715