WPF学习笔记(3)——x名称空间详解 中

x名称空间的标记扩展(Markup Extension)

1.  x:Type

x:Type的值应该是一个数据类型的名称。一般情况下,我们在编程中操作的是数据类型的实例或者是实例的引用,但有时候我们也会用到数据类型本身。我们对类(class)就行分析,它是具有双重身份的:在逻辑层面上,类是现实世界对象经过抽象和封装后的结果;在编程层面上,我们会使用这个类去创建对象和引用。以下为一用VS2013编写的实例:

   首先,创建了一个Button 的派生类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls; // Button

namespace Wpftest
{
    class MyButton:Button
    {
        public Type UserWindowType { get; set; }
        protected override void OnClick()
        {
            base.OnClick();
            MyWindow win = Activator.CreateInstance(this.UserWindowType) as MyWindow;
            if (win!=null)
            {
                win.ShowDialog();
            }
        }
    }
}
 再者,创建一个名为MyWindow的窗口,作为弹出时的样式,其UI设置如下,三个字符串输入,外加一个Button,目前他们的功能还没完善,

<Window x:Class="Wpftest.MyWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MyWindow" Height="300" Width="300">
    <StackPanel Background="LightBlue">
        <TextBox Margin="5"/>
        <TextBox Margin="5"/>
        <TextBox Margin="5"/>
        <Button Content="OK" Margin="5"/>
    </StackPanel>
</Window>
最后,在主窗口UI设置,

<Window x:Class="Wpftest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Wpftest"
        Title="MainWindow" Height="500" Width="900">
    <StackPanel>
        <local:MyButton Content="Show" UserWindowType="{x:Type TypeName=local:MyWindow}" Margin="5"/>
    </StackPanel>
</Window>

最后,解决方案的结构如图



2.  x:Null

在C#中,使用关键字null表示空值,中XAML里用x:Null表示空值。有时如果一个属性具有默认值,而我们又不需要这个默认值时就需要显式地设置null值了,以下附上一个关于使用了x:Null设置Button的程序,让其更形象生动得表示x:Null的作用:

<Window x:Class="Wpftest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="500" Width="900">
    <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"/>
        <Button Content="OK"/>
        <Button Content="OK" Style="{x:Null}"/>
    </StackPanel>
</Window>
其结果如下图:



3.标记扩展实例的两种声明语法

前面的x:Type 和 x:Null两个标记扩展,使用了他们的转义字符串式声明(即使用花括号括起来的字符串作为值赋给标签Attribute的形式)。因为标记扩展也是标准的.NET类,所以,我们也可以使用XAML标签来声明标记扩展的实例。如上述的x:Null例子,可以如下所写:

<Button Content="OK">
      <Button.Style>
            <x:Null/>
      </Button.Style>
</Button>
可以发现代码较之前复杂繁琐了许多。但是x:Array标记扩展,必须使用标签式声明才能做到。

4.  x:Array

x:Array的作用就是通过它的Items属性向使用者暴露一个类型已知的ArrayList实例,ArrayList内成员的类型由x:Array的Type指明,其实例如下:

<Window x:Class="Wpftest.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="500" Width="900">
     <Grid Background="LightBlue">
           <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>
运行后的结果如图显示:



5.  x:Static

功能是在XAML文档中使用数据类型的static成员,因XAML不能编写逻辑代码,故x:Static访问的static成员一定是数据类型的属性或字段。例子如下:

UI设置如下:

<Window x:Class="Wpftest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Wpftest"
        Title="{x:Static local:MainWindow.WindowTitle}" Height="500" Width="900">
    <StackPanel>
        <TextBlock FontSize="32" Text="{x:Static local:MainWindow.ShowText}"/>
    </StackPanel>
</Window>

C#如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 Wpftest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
       public MainWindow()
       {
           InitializeComponent();
       }
       public static string WindowTitle = "月黑风高";
       public static string ShowText
       {
           get
           {
               return "我是谁";
           }
       }
    }

}
结果如图:



结束,谢谢观看


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值