关于XAML名称空间的一些解释

本文详细介绍了XAML名称空间中的 xmlns:x 和 xmlns:d 的作用。xmlns:x 主要用于XAML语言解析和编译,提供XAML编译指令,如x:Class用于指定与C#代码的结合。而xmlns:d 则是设计视图相关的属性,仅在设计时有效,运行时被忽略,用于支持Visual Studio和Expression Blend的设计功能。

当我们新建一个WPF程序的时候,默认的WPF窗口都参考以下四个名称空间:

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"
<Window x:Class="WpfApplication1.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"
    Title="MainWindow" Height="350" Width="525">
    <Grid>

    </Grid>
</Window>

那么具体他们都是干什么的呢?

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

    首先,X名称空间映射的是http://schemas.microsoft.com/winfx/2006/xaml。看起来这个命名空间里面包含的类与解析XAML语言相关,所以我们可以将其称作XAML名称空间。与C#语言一样,XAML也有自己的编译器。XAML语言被解析并编译,最终形成微软中间语言保存在程序集中。在解析和编译XAML的过程中,我们经常要告诉编译器一些重要的信息,如XAML编译的结果应该和哪个C#代码编译的结果合并、使用XAML声明的元素是public还是private访问级别等等。这些让程序员能够与XAML编译器沟通的工具就存在X:名称空间中。


    我们注意到,它分为Attribute、标签扩展、XAML指令元素三个种类。下面我们讲讲它们的具体用法:

1.1  X名称空间中的Attribute

      我们应该理解,在XAML语言编程中,Attribute与Property是两个层面上的东西,Attribute是语言层面上的东西,是给编译器看的。

    Property属于面向对象理论范畴。在使用面向对象思想编程的时候,我们常常需要对客观事物进行抽象,再把抽象出来的结果 封装成类,类中用来表示事物状态的成员就是Property。比如我要写一个模拟赛车的游戏,那么必不可少的就是对现实汽车的抽象。现实中的汽车身上会带有很多数据,但在游戏中我可能只关心它的长度、宽度、高度、重量、速度等有限的几个数据,同时,我还会把汽车“加速”、“减速”等一些行为也提取出来并用算法模拟——这个过程就是抽象(结果是Car这个类)。显然,Car.Length、Car.Height、Car.Speed等表达的是汽车当前处在一个什么状态,而Car.Accelerate()、Car.Break()表达的是汽车能做什么。因此,Car.Length、Car.Height、Car.Speed就是Property的典型代表,将Property译为“属性”也很贴切。总结一句话就是:Property(属性)是针对对象而言的。

    Attribute则是编程语言文法层面的东西。比如我有两个同类的语法元素A和B,为了表示A与B不完全相同或者A与B在用法上有些区别,这时候我就要针对A和B加一些Attribute了。也就是说,Attribute只于语言层面上的东西相关——与抽象出来的对象没什么关系。因为Attribute是为了表示“区分”的,所以我喜欢把它译为“特征”。C#中的Attribute就是这种应用的典型例子——我们可以为一个类添加Attribute,这个类的类成员中有很多Property——显然Attribute只是用来影响类在程序中的用法而Property则对应着抽象对象身上的性状,它们根本不是一个层面上的东西。
    习惯上,英文中把标签式语言中表示一个标签特征的“名称-值”对称为Attribute。如果恰好我们又是用一种标签语言在进行面向对象编程,这时候两个概念就有可能混淆在一起了。实际上,使用能够进行面向对象编程的标签式语言只是把标签与对象做了一个映射,同时把标签的Attribute与对象的Property也做了一个映射——针对标签,我们还是叫Attribute,针对对象,我们还是叫Property,仍然不是一个层面上的东西。而且,标签的Attribute与对象的Property也不是完全映射的,往往是一个标签所具有的Attribute多于它所代表的对象的Property。

参考博文: 点击打开链接

      Property是面向对象层面的东西,是给编程逻辑看的。而且一个标签中的Attribute大部分对应对象的Property。在使用XAML编程的时候,如果你想给它加一点特殊的标记来改变XAML对它的解析,这时候就需要额外的给它添加一些Attribute了。比如,你想告诉XAML编译器将哪个编译结果和那个C#编译的类合并,这时候就必须为这个标签添加X:Class  Attribute来告诉编译器。X:Class并不是对象成员,而是重X空间硬贴上去的。举例来说,我们看一下常用的Attribute,x:Class。
        这个Attribute是告诉XAML编译器将XAML编译器编译的结果和后台编译结果的哪一个类进行合并,使用x:Class有以下几点要求:
  • 这个Attribute只能用于根节点
  • 使用x:Class的根节点的类型要与x:Class的值所指示的一致。
  • x:Class的值所指示的类型在声明的时候必须使用partial关键字。
     以上内容参考博文:   点击打开链接

2  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

     之前看到一篇帖子,有人问下面的代码是什么意思?
  
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="725" d:DesignWidth="1015" 

    下面有人回答:

      这是设计视图下的属性,就是说你在设计视窗里看到的高度和宽度分别为725和1015,但是此两项属性与你运行后的程序是无关的,它们在编译过程中是被忽略的。这无非就是帮助设计者减轻一点工作而已。界面设计和程序设计相分离,才出现mc:Ignorable="d",就是在编译的时候忽略这个?

我觉得说的蛮有道理的,下面是StackOverFlow上面的一些解释:

d: (http://schemas.microsoft.com/expression/blend/2008)

The d: XAML namespace is intended for designer support, specifically designer support in the XAML design surfaces of Microsoft Visual Studio and Microsoft Expression Blend. The d: XAML namespace enables designer attributes on XAML elements. These designer attributes only affect the design aspects of how XAML behaves. The designer attributes are ignored when the same XAML is loaded by the XAML parser in the Silverlight run-time, and the application runs. Generally, the designer attributes are valid on any XAML element, but in practice there are only certain scenarios where applying a designer attribute yourself is appropriate.

mc: (http://schemas.openxmlformats.org/markup-compatibility/2006)

mc: indicates and supports a markup compatibility mode for reading XAML. Typically, the d: prefix is associated with the attribute mc:Ignorable. This technique enables run time XAML parsers to ignore the design attributes, as described previously.

From my understanding, “d”namespace enables designer-only attributes in your code. This is so you can add stuff like design time data to your application and make it blendable.

The “mc” namespace supports compatibility and usually the “d”prefix mentioned above is paired with an “mc:ignorable” which tells the compiler to ignore the design time elements at runtime

参考博文: 点击打开链接


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值