轉自:http://blog.csdn.net/lisenyang/article/details/18312039
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>??
- 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";??
- ???????????}??
- ???????}??
- <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>??
- <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>??
- string?str?=?this.FindResource("myString")?as?string;??
- this.label1.Content?=?str;??
- <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>??
- UserWindowType="{x:Type?local:Window1}"??
- <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>??
- 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
