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();
}
}
}
}

添加一个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>

再编写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>

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>

效果:

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>

但如果这样写,是没有数据可以提供的,则改为标签声明语法:

<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>

结果为:

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();
}
}
}

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>

结果:

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>

x名称空间中的标记拓展的更多相关文章

  1. x名称空间中的内容

    原文:x名称空间中的内容 x名称空间映射的是http://schemas.microsoft.com/winfx/2006/xaml,它包含的类均与XAML的解析有关,下面分三部分介绍 一:x名称空间 ...

  2. 9、XAML名称空间详解

    XAML命名空间 <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"      ...

  3. Spring的2.5版本中提供了一种:p名称空间的注入(了解)

    1. 步骤一:需要先引入 p 名称空间 * 在schema的名称空间中加入该行:xmlns:p="http://www.springframework.org/schema/p"( ...

  4. [No000013F]WPF学习之X名称空间详解

    X名称空间里面的成员(如X:Name,X:Class)都是写给XAML编译器看的.用来引导XAML代码将XAML代码编译为CLR代码. 4.1X名称空间里面到底都有些什么? x名称空间映射的是:htt ...

  5. 《C++ Primer Plus》读书笔记之七—内存模型和名称空间

    第九章 内存模型和名称空间 1.不要将函数定义或者变量声明放到头文件中. 2.头文件常包含的内容:函数原型.使用#define或者const定义的常量.结构声明.类声明.模板声明.内联函数. 3.避免 ...

  6. WPF学习之X名称空间详解

    X名称空间里面的成员(如X:Name,X:Class)都是写给XAML编译器看的.用来引导XAML代码将XAML代码编译为CLR代码. 4.1X名称空间里面到底都有些什么? x名称空间映射的是:htt ...

  7. x名称空间

    XAML代码的WPF程序都需要通过语句:xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml",x就是用来映射xmlns:x= ...

  8. C++ 名称空间

    在程序中,只使用 using namespace std; 而不使用其他的名称空间,如using namespace boost; 这样的好处有: 1.可以避免不同名称空间中的名称冲突: 2.可以很清 ...

  9. 理解C#系列 / 核心C# / 名称空间

    名称空间namespace 名称空间 名称空间用来逻辑分类,而不是物理上的,名称空间与程序集无关[程序集:经由编译器编译得到的文件],同一个程序集中可以有2不同的名称空间,也可以在不同的程序集中定义同 ...

随机推荐

  1. href="#"会导致location.replace(location.href);脚本不工作

    我们经常这样:<a onclick="xxx" href="#" 其实这不是一个好习惯,当点下这个连接后,主页面的URL后面会加个#号,这样就会导致很多J ...

  2. restful 注解

    @Path @Path 注释被用来描述根资源.子资源方法或子资源的位置.value 值可以包含文本字符.变量或具有定制正则表达式的变量. @GET.@POST.@PUT.@DELETE.@HEAD @ ...

  3. phpize 动态添加 PHP 扩展的错误及解决方案

    使用phpize 动态添加 PHP 扩展是开发中经常需要做的事情,但是在 macOS 中,首次使用该功能必然会碰到一些错误,本文列出了这些错误的解决方法. 问题一: 执行 phpize 报错如下: $ ...

  4. .Net额外小工具

    http://blog.csdn.net/zhumi/article/details/49076 用于编写单元测试的 NUnit用于创建代码文档资料的 NDoc用于生成解决方案的 NAnt用于生成代码 ...

  5. java final .static修饰符

    static静态修饰符(使用static修饰符修饰的成员变量.常量.和成员方法成为静态变量.常量和方法,他们统称为静态成员,归整个类所有,不依赖于类的特定实例,被类的所有实例共享,只要这个类被加载,J ...

  6. string xml json格式区别

    string 是一种最普通的储存一串字符的数据格式 xml 是一种可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 它非常适合万维网传输,提供统一的方 ...

  7. Power Network 分类: POJ 2015-07-29 13:55 3人阅读 评论(0) 收藏

    Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 24867 Accepted: 12958 Descr ...

  8. 验证码识别--type7

    验证码识别--type7 一.干扰分析 有黑色边框,然后点干扰,线干扰 去边框 去点干扰 变成这样的结果,方便运算吗?也可以多种方式联合起来运算的.我相信在很多情况下,都可能会遇到类似的结果.我们人类 ...

  9. IOCTL函数用法

    http://blog.163.com/he_junwei/blog/static/19793764620152510533753/ http://blog.csdn.net/styyzxjq2009 ...

  10. Thinkphp3.2.3如何加载自定义函数库

    方法一:将自定义函数库放在Common文件夹下的Common文件夹下,命名为function.php. 方法二:项目配置文件中定义LOAD_EXT_FILE参数.这个方法在3.1的开发手册中有. 参考 ...