“x”名稱空間的成員引導(dǎo)XAML編譯器將XAML編譯成CLR代碼的∑谰兀“x”就XAML首字母篮愉。凡包含XAML代碼的WPF程序都需要通過語句 xmlns: x="http:// schemas.microsoft.com/winfx/ 2006/xaml"
引入耻矮。此名稱空間是程序員與XAML編譯器溝通的工具初厚。
x名稱空間都有什么東東
x名稱空間有Attribute、標(biāo)記擴(kuò)展和XAML指令元素組成郑叠。如下表夜赵。
名稱 | 種類(XAML出現(xiàn)的形式) |
---|---|
x:Array,x:Null,x:Static,x:Type | 標(biāo)記擴(kuò)展 |
x:Code,x:XData | XAML指令元素 |
x:Key,x:Name,x:Class,x:FeildModifer | Attribute |
x:ClassModifer,x:Shared,x:TypeArgument,x:Uid | Attribute |
Attribute
Attribute:語言層面的東西, 是給編譯器看的乡革。
Property:對象上面的東西寇僧,是給編程邏輯用的。
XAML標(biāo)簽的Attribute大部分對應(yīng)對象的Property沸版。在XAML編程中嘁傀,你想添加特殊的標(biāo)記從而影響XAML編譯器的解析,這時候就要添加一些Attribute了视粮。
<window x:Class="ClassName">
<!--告訴XAML編譯器將編譯結(jié)果與哪個C#編譯的類合并-->
</window>
x:Class
x:Class
告訴XAML編譯器將XAML標(biāo)簽的編譯結(jié)果與后臺代碼中指定的類合并细办。在使用時遵循以下要求:
- 只能用于根結(jié)點(diǎn)(如:window、UserControl)
- 使用
x:Class
的根結(jié)點(diǎn)的類型與x:Class的值所指示的類型保持一致 -
x:Class
所指示的類型在聲明時必須用partial關(guān)鍵字
x:ClassModifier
x:ClassModifier
告訴XAML編譯器標(biāo)簽編譯生成類的訪問級別蕾殴。這個Attribute需要注意以下幾點(diǎn):
- 標(biāo)簽必須具有
x:Class
-
x:ClassModifier值與
x:Class`所指類的訪問級別一致 -
x:ClassModifier
與后臺代碼的不同而不同笑撞,參見TypeAttributes
例:x:ClassModifier="internal"
x:Name
x:Name
給標(biāo)簽對象起個名子,也就是生成對象實(shí)例并為它聲明一個引用變量钓觉,通過這個變量方便C#代碼調(diào)用這個實(shí)例茴肥。
注意:
- 如果一個標(biāo)簽的對象有Name屬性,為方便查找將
x:Name
與Name
賦值相同并注冊在UI樹上荡灾。 - Name屬性WPF控件的基類屬性瓤狐,所有控件都有Name屬性瞬铸。
- 當(dāng)一個對象有Name屬性,使用
x:Name
和Name
等同础锐。為提高代碼統(tǒng)一性嗓节,統(tǒng)一使用x:Name
<StackPanel>
<Button x:Name="button1" Margin="5"/>
<TextBox x:Name="textBox1" Margin="5"/>
</StackPanel>
x:FeildModifier
x:FeildModifier
改變引用變量的訪問級別。使用前必須通過x:Name
聲明引用變量郁稍。
<Button x:Name="btn1" x:FeildModifier="public" Content="Click Me" />
x:Key
x:Key
Resource(資源)檢索的索引赦政。
<Window.Resources>
<sys:String x:Key="Hello World">Hello World!</sys:String>
</Window.Resources>
<StackPanel>
<TextBlock Text="{StaticResource ResourceKey=Hello World}"/>
</StackPanel>
x:Shared
x:Shared
和x:Key
配合使用胜宇。當(dāng)x:Shared="true"
通過x:Key
檢索的對象為同一個對象耀怜。如果x:Sharp="false"
通過x:Key
檢索的對象是此對象的新副本。
標(biāo)記擴(kuò)展
標(biāo)記擴(kuò)展(Markup Extension)實(shí)際上就是MarkupExtension類直接或間接的派生類桐愉。
x:Type
x:Type
的值代表一個數(shù)據(jù)類型的名稱财破。一般情況下,我們在編程中操作著數(shù)據(jù)類型的實(shí)例或是實(shí)例的引用从诲。但有些時候我們還會用到數(shù)據(jù)類型本身左痢。
在XAML中表達(dá)一個數(shù)據(jù)類型就要用到x:Type
。比如一個類的屬性是一個數(shù)據(jù)類型系洛,當(dāng)給此類賦值時俊性。引用一下書中的例子
首先創(chuàng)建一個Button
的派生類MyButton
,里面包含一個名為UserWindowType
的Type類型的屬性描扯。
public class MyButton : Button
{
public Type UserWindowType { get; set; }
protected override void OnClick()
{
base.OnClick();
Window win = Activator.CreateInstance(this.UserWindowType) as Window;
if (win != null)
{
win.ShowDialog();
}
}
}
然后創(chuàng)建一個Window
的一個派生類MyWindow
定页。
<Window x:Class="SamplesForWPF.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:SamplesForWPF"
mc:Ignorable="d"
Title="MyWindow" Height="150" Width="300">
<StackPanel Background="LightBlue">
<TextBox Margin="5"/>
<TextBox Margin="5"/>
<TextBox Margin="5"/>
<Button Content="OK" Margin="5"/>
</StackPanel>
</Window>
最后把自定義按鍵MyButton
放在主窗口,并將MyWindow
賦值給MyButton.UserWindowType
绽诚。
<Window x:Class="SamplesForWPF.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:SamplesForWPF"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="400">
<StackPanel Background="LightBlue">
<local:MyButton Content="Show" UserWindowType="{x:Type TypeName=local:MyWindow}" Margin="5"/>
</StackPanel>
</Window>
TypeExtension類的構(gòu)造器支持類型名參數(shù)典徊,所以可以寫成
UserWindowType="{x:Type local:MyWindow}"
x:Null
x:Null
與C#的Null
意義相同,代表一個空值恩够。在XAML中卒落,如果給某個對象的屬性賦一個空值,我們可以這樣寫
<Button Content="OK" Style="{x:Null}"/>
標(biāo)記擴(kuò)展的兩種聲明語法
常用的方法用朱{}
括起來
<Button Content="OK" Style="{x:Null}"/>
另外一種語法如下
<Button Content="OK">
<Button.Style>
<x:Null/>
</Button.Style>
</Button>
大多數(shù)情況我們都會使用第一種語法蜂桶,看起不來簡潔明了儡毕。有一個另外,就是x:Array
扑媚。
x:Array
x:Array
的作用就是通過它Items屬性向使用者暴露一個類型已知的ArrayList腰湾。ArrayList的成員類型由x:Array
的Type屬性指明。
<ListBox Margin="5" ItemsSource="{x:Array Type=sys:String}"/>
一個String類型的ArrayList作為Data Source賦給了ListBox钦购。此時的ArrayList是一個空的檐盟,我們要通個下面的代碼向Items添加數(shù)據(jù)
<ListBox Margin="5">
<ListBox.ItemsSource>
<x:Array Type="sys:String">
<sys:String>張三</sys:String>
<sys:String>李四</sys:String>
</x:Array>
</ListBox.ItemsSource>
</ListBox>
在解析< x: Array>
標(biāo)簽 的 時候編譯器會生成調(diào)用AddChild
方法的代碼把< x: Array>
標(biāo)簽 的子元素逐個添加到x: Array
實(shí)例的Items
里。
x:Static
x:Static
是用來使用XAML文檔中數(shù)據(jù)類型的靜態(tài)成員押桃。XAML不能編寫邏輯代碼葵萎,所以使用x:Static
訪問數(shù)據(jù)類型的靜態(tài)成員一定是屬性和字段。引用書中的例子
public partial class MainWindow : Window
{
public static string WindowTitle = "山高月小";
public static string ShowText { get { return "水落石出"}; }
public MainWindow()
{
InitializeComponent();
}
}
然后用x:Static
訪問MainWindow.ShowText
<Window x:Class="SamplesForWPF.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:SamplesForWPF"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="400">
<StackPanel Background="LightBlue">
<TextBlock Text="{x:Static local:MainWindow.ShowText}"/>
</StackPanel>
</Window>
XAML指令元素
XAML指令元素只有兩個,x:Code
,x:XData
羡忘。
x:Code
x:Code
作用是可以包含本應(yīng)后置的C#代碼谎痢。
x:XData
x:XData
是一個專用標(biāo)簽。數(shù)據(jù)提供者XmlDataProvider
的實(shí)例放在x:XData
內(nèi)容里卷雕。
引用書中實(shí)例