《深入浅出WPF》4.0x名称空间

4.1 x名称空间

定义:映射到XML名称空间的取名

作用:引导XAML编译器将XAML代码编译为CLR代码,XAML代码都需要引用此空间

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

x名空间包含的工具

分类为:Attribute、标签扩展、XAML指令元素

名称 种类(在XAML中出现的形式)
x:Array 标签扩展
x:Class Attribute
x:ClassModifier Attribute
x:Code XAML指令元素
x:FileModifier Attribute
x:Key Attribute
x:Name Attribute
x:NULL 标签扩展
x:Shared Attribute
x:Static 标签扩展
x:SubClass Attribute
x:Type 标签扩展
x:TypeArguements Attribute
x:Uid Attribute
x:XData XAML指令元素

4.2 x名称空间的Attribute

Attribute和Property区别:Attribute 是语言层面的,给编译器看的,Property面向对象,给编程逻辑看的

4.2.1 x:Class 合并类

作用:这个Attribute是将XAML编译出的结果和与后台代码中指定的类合并

x:Class="WpfAppTest3.MainWindow"
## 注意
* 这个Attribute只能用于根节点
* 使用x:Class 的根节点类型要与x:Class所指定的根节点类型一致
* x:Class所指示的类型在声明的时候必须使用partial关键字

4.2.2 x:ClassModifier Class访问级别

x:ClassModifier :Attribute 告诉XAML编译器由标签生成的类的访问级别。

## 注意事项
* 标签必须有x:Class Attribute
* x:ClassModifier的访问级别必须与x:class所指定的类的访问级别一致
* x:ClassModifier 的值随着后台代码的语言类型的不同而不同
//C#
internal partial class MainWindow : Window
<!--XAML中-->
<x:ClassModifier="internal"/>

4.2.3 x:Name

XAML标签声明的是对象

通过层级索引

<Window x:Class="WpfApp1XName.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:WpfApp1XName"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <TextBox Margin="5"/>
        <Button Content="OK" Margin="5" Click="Button_Click"/>
    </StackPanel>
</Window>
private void Button_Click(object sender, RoutedEventArgs e)
{
    StackPanel stackPanel = this.Content as StackPanel;
    TextBox textBox = stackPanel.Children[0] as TextBox;
    if (string.IsNullOrEmpty(textBox.Name))
    {
        textBox.Text = "No Name";
    }
    else
    {
        textBox.Text = textBox.Name;
    }
}

x:Name作用1:告诉XAML编译器,为实例声明一个引用变量,引用变量的名称就是x:Name的值

x:Name作用2:将XAML标签的Name属性(如果有)也设置为x:Name的值,并把这个值注册到UI树上,方便查找

通过X:Name索引

<StackPanel>
    <TextBox x:Name="textbox1" Margin="5"/>
    <Button Content="OK" Margin="5" Click="Button_Click"/>
</StackPanel>
private void Button_Click(object sender, RoutedEventArgs e)
{
    if (string.IsNullOrEmpty(this.textbox1.Name))
    {
        this.textbox1.Text = "No Name";
    }
    else 
    {
        this.textbox1.Text = this.textbox1.Name;
    }
}
## 注意
* Name属性定义在FrameWorkElement类中,这个类是WPF控件的基类,所有WPF控件都有Name属性
* x:Name的功能涵盖Name的功能

4.2.4 x:FiledModifier 变量访问级别

x:FiledModifier 设置变量的访问级别,默认下变量的访问级别是internal

<TextBox x:Name="textbox2" Margin="5" Text="FiledModifier" x:FieldModifier="public"/>
## 注意
* 使用x:FiledModifier设置属性时必须使用x:Name定义变量名

4.2.5 x:Key 检索资源关键字

作用:使用Key检索资源字典,检索资源

资源:Resources,需要重复使用的XAML的内容,可以放置style、Template、动画等。

key 定义资源:

    <Window.Resources>
        <sys:String x:Key="myString">Hello Wpf Resources!</sys:String>
    </Window.Resources>

调用资源:

<TextBox Text="{StaticResource ResourceKey=myString}" Margin="5"/>
 string str = this.FindResource("myString") as string;

整体代码:

<Window x:Class="WpfApp1XName.Window2Key"
        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:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:WpfApp1XName"
        mc:Ignorable="d"
        Title="Window2Key" Height="450" Width="800">
    <Window.Resources>
        <sys:String x:Key="myString">Hello Wpf Resources!</sys:String>
        <sys:Double x:Key="DoubleValue1">2</sys:Double>
    </Window.Resources>
    <StackPanel>
        <TextBox Text="{StaticResource ResourceKey=myString}" Margin="5"/>
        <TextBox x:Name="textbox1" Margin="5"/>
        <Button Content="Show" Click="Button_Click" Margin="5"/>
        <Slider Value="{StaticResource ResourceKey=DoubleValue1}"/>
    </StackPanel>
</Window>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string str = this.FindResource("myString") as string;
            this.textbox1.Text = str;
        }

4.2.6 x:Shared 资源属性

定义:配合Key 使用,设置引用资源时是引用资源的同一个对象还是引用对象的副本。

设置为TRUE时,代表使用的是同一个对象,默认设置为TRUE。

设置为FALSE 时,代表使用的是副本,代表每次检索到这个对象时,我们得到的都是这个对象的副本。

4.3 x名称空间的标记扩展

标记扩展(Markup Extension)是MarkupExtension类的直接或间接派生类

4.3.1 x:Type 数据类型名称

x:Type : 值为数据类型的名称,可以赋值类,枚举,结构。

类、结构、枚举都是数据类型(Type)

Type进行实例化,使用Activator.CreateInstance函数实例化Type数据类型

Window win = Activator.CreateInstance(this.UserWindowType) as Window;

Type 赋值

  <!--赋值语句 -->
  <local:MyButton UserWindowType="{x:Type TypeName=local:MyWindow1}" Margin="5"/>
  <local:MyButton UserWindowType="{x:Type local:MyWindow1}" Margin="5"/>

Window1

<Window x:Class="WpfApp3Type.MyWindow1"
        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:WpfApp3Type"
        mc:Ignorable="d"
        Title="MyWindow1" Height="450" Width="800">
    <StackPanel Background="Blue">
        <TextBox Margin="5"/>
        <TextBox Margin="5"/>
        <TextBox Margin="5"/>
        <Button Content="OK" Margin="5"/>
    </StackPanel>
</Window>
    public class MyButton : Button
    {
        public Type UserWindowType { get; set; }       
        protected override void OnClick()
        {
            base.OnClick();
            //Window win = this.UserWindowType as Window;
            Window win = Activator.CreateInstance(this.UserWindowType) as Window;
            if (win != null)
            {
                win.ShowDialog();
            }
        }
    }

Window2

<Window x:Class="WpfApp3Type.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:WpfApp3Type"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <local:MyButton Content="show1" UserWindowType="{x:Type TypeName=local:MyWindow1}" Margin="5"/>
        <local:MyButton Content="show2" UserWindowType="{x:Type local:MyWindow1}" Margin="5"/>
        <Button x:Name="btn1" Content="Test2" Margin="5" Click="btn1_Click"/>
    </StackPanel>
</Window>

4.3.2 x:Null 属性赋空值

作用:为属性赋空值

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

Style 样式

作用:可以使用Style样式,设置一组控件属性,可指定控件类型,应用到这一类型的所有控件;也可单独设置控件属性。

  • 方式1:应用到这一类型的所有控件

    初始化:

    <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>
    
    ### 注意:
    <Style x:Key="{x:Type Button}"  TargetType="{x:Type Button}">
    可简写为:
    <Style TargetType="Button">
    

    使用

    <StackPanel>
        <Button Content="OK"/>
        <Button Content="OK"/>
        <Button Content="OK"/>
        <Button Content="OK" Style="{x:Null}"/>
    </StackPanel>
    
  • 方式2:应用到控件

    初始化:

    <Window.Resources>
        <Style x:Key="Button1" TargetType="{x:Type Button}">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Height" Value="36"/>
            <Setter Property="Margin" Value="5"/>
        </Style>
    </Window.Resources>
    
    ### 注意:
    <Style x:Key="Button1" TargetType="{x:Type Button}">
    可简写为:
    <Style  x:Key="Button1" TargetType="Button">
    

    使用

    <StackPanel>
        <Button Content="OK"/>
        <Button Content="OK"/>
        <Button Content="OK"/>
        <Button Content="OK" Style="{StaticResource Button1}"/>
        <Button Content="OK" Style="{x:Null}"/>
    </StackPanel>
    
  • 样式扩展

    可以进行继承

    <Window.Resources>
        <!-- .... other resources .... -->
    
        <!--A Style that affects all TextBlocks-->
        <Style TargetType="TextBlock">
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="FontFamily" Value="Comic Sans MS"/>
            <Setter Property="FontSize" Value="14"/>
        </Style>
        
        <!--A Style that extends the previous TextBlock Style with an x:Key of TitleText-->
        <Style BasedOn="{StaticResource {x:Type TextBlock}}"
               TargetType="TextBlock"
               x:Key="TitleText">
            <Setter Property="FontSize" Value="26"/>
            <Setter Property="Foreground">
                <Setter.Value>
                    <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                        <LinearGradientBrush.GradientStops>
                            <GradientStop Offset="0.0" Color="#90DDDD" />
                            <GradientStop Offset="1.0" Color="#5BFFFF" />
                        </LinearGradientBrush.GradientStops>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    

4.3.3 标记扩展两种声明方式

  • 方式1 用花括号声明

      <Button Content="OK" Style="{StaticResource Button1}"/>
    
  • 方式2 使用标签声明

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

4.3.4 x:Array 使用

作用:通过他的Items向他的使用者暴露已知的ArrayList实例,ArrayList成员类型由Type决定

向使用者提供ArrayList

<ListBox Margin="5">
    <ListBox.ItemsSource>
        <x:Array Type="sys:String">
            <sys:String>ASD</sys:String>
            <sys:String>QWE</sys:String>
        </x:Array>
    </ListBox.ItemsSource>
</ListBox>
## x:Array只能使用标签形式申明

字段和属性

字段:又称为:“成员变量”,一般在类的内部做数据交互使用。

属性:在面向对象设计中主要使用属性描述对象的静态特征。

class SetStudent
{
    //字段
    private string student = "name1";
    //属性
    public string Student
    {
        set { student = value; }
        get { return student; }
    }
}

4.3.5 x:Static

作用:使用x:static 标记扩展访问static成员

申明

 //字段
public static string student = "name1";
//属性
public static string Student
{
    set { student = value; }
    get { return student; }
}

使用

Title="{x:Static local:Window4.student}" 
<TextBox Text="{x:Static local:Window4.Student}"/>

4.4 XAML指令元素

x:Code 将代码放置到XAML中

<Grid>
    <Button x:Name="button1" Content="OK" Click="Button1_Click" Margin="5"/>  
</Grid>
<x:Code>
    <![CDATA[
            private void Button1_Click(object sender,RoutedEventArgs e)
            {
                MessageBox.Show("OK");
            }   
            ]]>
</x:Code>

x:XData 存放XmlDataProvider的内容数据

声明

# 注意XPath的路径要在x:Xdata中
<Window.Resources>
    <XmlDataProvider x:Key="InventoryData" XPath="Inventory/Books">
        <x:XData>
            <Inventory xmlns="">
                <Books>
                    <Fruit Name="Peach">ABCD</Fruit>
                    <Fruit Name="Banana">A1BCD</Fruit>
                    <Fruit Name="Orange">A2BCD</Fruit>
                </Books>
                <Drinks>
                    <Drink Name="Coca cola">A3BCD</Drink>
                    <Drink Name="PEPSI Cola">A4BCD</Drink>
                </Drinks>
            </Inventory>
        </x:XData>
    </XmlDataProvider>
</Window.Resources>

使用:

<ListBox>
    <ListBox.ItemsSource>
        <Binding Source="{StaticResource InventoryData}"/>
    </ListBox.ItemsSource>
</ListBox>
原文地址:https://www.cnblogs.com/ycccq/p/13491154.html