文章出處

 1.x:Type

 Type類可作為所有數據類型在編程層面上的抽象。在XAML中,如果想表達某個數據類型時就需要使用x:Type標記拓展。例子:

創建一個Button的派生類:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication3
{
    class MyButton:Button
    {
        public Type UserWindowType { get; set; }
        protected override void OnClick()//是protected,不是public
        {
            base.OnClick();//激發Click事件
            Window win = Activator.CreateInstance(this.UserWindowType) as Window;
            if (win != null)
            {
                win.ShowDialog();
            }
        }
    }
}
View Code

 

添加一個MyWindow:

<Window x:Class="WpfApplication3.MyWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        WindowStartupLocation="CenterScreen"
        Title="MyWindow" Height="170" Width="200">
    <Grid>
        <StackPanel Background="LightBlue">
            <TextBox Margin="5"></TextBox>
            <TextBox Margin="5"></TextBox>
            <TextBox Margin="5"></TextBox>
            <Button Content="OK" Margin="5"></Button>
        </StackPanel>
    </Grid>
</Window>
View Code

 

再編寫MainWindow:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication3"
        WindowStartupLocation="CenterScreen"
        Title="MainWindow" Height="300" Width="300">
    <!--xmlns:local="clr-namespace:WpfApplication3"引入命名空間WpfApplication3,并映射為local-->
    <Grid>
        <StackPanel>
            <local:MyButton Content="Show" UserWindowType="{x:Type TypeName=local:MyWindow}" Margin="5"></local:MyButton>
        </StackPanel>
    </Grid>
</Window>
View Code

 

2.x:Null

顯示地對一個屬性賦一個空值

例子:

MainWindow:

<Window x:Class="xNull.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="x:Null Sample" Height="300" Width="300">
    <Window.Resources>
        <Style x:Key="{x:Type Button}" TargetType="{x:Type Button}">
            <Setter Property="Width" Value="60"></Setter>
            <Setter Property="Height" Value="36"></Setter>
            <Setter Property="Margin" Value="5"></Setter>
        </Style><!--設置Button控件的默認Style-->
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Button Content="0K"></Button>
            <Button Content="0K"></Button>
            <Button Content="0K"></Button>
            <Button Content="OK" Style="{x:Null}"></Button><!--顯示地把Style設置為x:Null了-->
        </StackPanel>
    </Grid>
</Window>
View Code

 

效果:

 

 

3.標記擴展實例的兩種聲明語法

 上面的例子,可以寫成:

<Button Content="OK">
    <Button.Style>
        <x:Null />
    </Button.Style>
</Button>

 

這樣寫,代碼顯得有些啰嗦,所以為了保持簡潔,很少使用這樣的寫法 

 

4.x:Array

 x:Array的作用就是通過它的Items屬性向使用者暴露一個類型已知的ArrayList實例,ArrayList內成員的類型由x:Array的Type指明

例子:

<Window x:Class="xArray.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="120" Width="160">
    <!--需要使用String,引入xmlns:sys="clr-namespace:System;assembly=mscorlib"-->
    <Grid>
        <Grid Background="LightBlue">
            <ListBox Margin="5" ItemsSource=“{x:Array Type=sys:String}”><!--類型為String-->
            </ListBox>
        </Grid>
    </Grid>
</Window>
View Code

 

但如果這樣寫,是沒有數據可以提供的,則改為標簽聲明語法:

<Window x:Class="xArray.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="120" Width="160">
    <!--需要使用String,引入xmlns:sys="clr-namespace:System;assembly=mscorlib"-->
    <Grid>
        <Grid Background="LightBlue">
            <ListBox Margin="5">
                <ListBox.ItemsSource>
                    <x:Array Type="sys:String"><!--類型為String-->
                        <sys:String>Tim</sys:String>
                        <sys:String>Tom</sys:String>
                        <sys:String>Victor</sys:String>
                    </x:Array>
                </ListBox.ItemsSource>
            </ListBox>
        </Grid>
    </Grid>
</Window>
View Code

結果為:

 

 5.x:Static

 在XAML文檔中使用數據類型的Static成員

例子:

MainWindow.xaml.cs:

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 xStatic
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public static string WindowTitle = "高山流水";
        public static string ShowText { get { return "水落石出"; } }

        public MainWindow()
        {
            InitializeComponent();
        }
    }
}
View Code

 

 

MainWindow.xaml

<Window x:Class="xStatic.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:xStatic"
        Title="{x:Static local:MainWindow.WindowTitle}" Height="120" Width="300">
    <!--把命名空間xStatic映射為local-->
    <!--使用x:Static訪問靜態成員數據-->
    <Grid>
        <StackPanel>
            <TextBox FontSize="32" Text="{x:Static local:MainWindow.ShowText}"></TextBox>
            <!--使用x:Static訪問靜態成員數據-->
        </StackPanel>
    </Grid>
</Window>
View Code

 

 結果:

 

6.XAML指令元素

 XAML指令元素有兩個:

x:Code

x:XData

1)x:Code的作用是,放置一些本應該放在后置代碼中的C#代碼。一般不會這樣做,這樣做,會是代碼難以維護、不易于調試

2)x:XData是一個專用標簽。如果想在XAML中聲明一個帶有數據的XmlDataProvider實例,那么XmlDataProvider實例的數據就應該放在x:Data標簽的內容里,比如:

<Window.Resources>
    <XmlDataProvider x:Key="InventoryData" XPath="Inventory/Books">
        <x:XData>
            <Supermarket xmlns="">
               <Fruits>
                  <Fruit Name="Peach"/>
                  <Fruit Name="Banana"/>
                  <Fruit Name="Orange"/>
               </Fruits>
               <Drinks>
                  <Drink Name="Coca Cola"/>
                  <Drink Name="PEPSI Cola"/>
               </Drinks>
            </Supermarket>    
        </x:XData>
    </XmlDataProvider>
</Window.Resources>

 

 

 

 

 

 

 

 


文章列表


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

    IT工程師數位筆記本

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