文章出處

XAML代碼的WPF程序都需要通過語句:xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml",x就是用來映射xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml",它包含的類均與解析XAML語言相關,所以亦稱為"XAML名稱空間"

x名稱空間中包含的工具:

 1.x:Name

 x:Name的作用:

1)告訴XAML編譯器,當一個標簽帶有x:Name時除了為這個標簽生成對應實例外還要為這個實例聲明一個引用變量,變量名就是x:Name的值

2)將XAML標簽所對應對象的Name屬性(如果有)也設為x:Name的值,并把這個值注冊到UI樹上,以方便查找

 

2.x:FieldModifier

默認情況下,字段的訪問級別是internal。x:FieldModifier的作用就是在XAML里改變引用變量的訪問級別,例如:

<TextBox x:Name="textBox1" x:FieldModifier="public" HorizontalAlignment="Left" Height="23" Margin="64,26,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="textBox2" x:FieldModifier="public" HorizontalAlignment="Left" Height="23" Margin="64,74,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="textBox3" HorizontalAlignment="Left" Height="23" Margin="64,124,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/> 

 前兩個的訪問級別改為public,最后一個的訪問級別為internal

 

3.x:Key

對應于"Key-Value" 的檢索方式。在XAML文件中,可以把多次使用的資源提取出來,放到資源字典(Resource Dictionary)里,需要使用這個資源的時候,就把Key檢索出來。x:Key就是用于為資源貼上用于檢索的索引

例子:

Foreground:

<Window x:Class="xKey.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"
        Title="MainWindow" Height="130" Width="200">
    <!--要使用String,所以要添加:xmlns:sys="clr-namespace:System;assembly=mscorlib"(直接復制黏貼過來就好)-->
    <Window.Resources>
        <sys:String x:Key="myString">Hello WPF Resources!</sys:String>
    </Window.Resources>
    <Grid>
        <StackPanel Background="Gray">
            <TextBox Text="{StaticResource ResourceKey=myString}" Margin="5"></TextBox><!--使用x:Key中的資源-->
            <TextBox x:Name="textBox1" Margin="5"></TextBox>
            <Button Content="Show" Click="Button_Click" Margin="5"></Button>
        </StackPanel>
    </Grid>
</Window>

 

 Background:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace xKey
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string str = this.FindResource("myString") as String;//調用資源
            this.textBox1.Text = str;
        }
    }
}

 

4.x:Share

 在使用x:Key時,一旦把某些對象當做資源放進資源字典后就可以把它們檢索出來重復使用。但問題是,它們是不是都是同一個對象呢?這就看給x:Share賦了什么值了。x:Share一定要與與x:Key配合使用,如果x:Share的值為true,則每次檢索這個對象時,都是同一個對象,否則,如果為false,則得到的是這個對象的一個新副本。默認情況下,得到的都是同一個對象,即x:Share的值為true

 

 

 

 

 

 

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()