WPF學習筆記:x名稱空間詳解

字母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的初衷的。

 

 

 

發佈了38 篇原創文章 · 獲贊 8 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章