当我们新建一个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>
那么具体他们都是干什么的呢?
1 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
Property属于面向对象理论范畴。在使用面向对象思想编程的时候,我们常常需要对客观事物进行抽象,再把抽象出来的结果 封装成类,类中用来表示事物状态的成员就是Property。比如我要写一个模拟赛车的游戏,那么必不可少的就是对现实汽车的抽象。现实中的汽车身上会带有很多数据,但在游戏中我可能只关心它的长度、宽度、高度、重量、速度等有限的几个数据,同时,我还会把汽车“加速”、“减速”等一些行为也提取出来并用算法模拟——这个过程就是抽象(结果是Car这个类)。显然,Car.Length、Car.Height、Car.Speed等表达的是汽车当前处在一个什么状态,而Car.Accelerate()、Car.Break()表达的是汽车能做什么。因此,Car.Length、Car.Height、Car.Speed就是Property的典型代表,将Property译为“属性”也很贴切。总结一句话就是:Property(属性)是针对对象而言的。
- 这个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
参考博文: 点击打开链接
本文详细介绍了XAML名称空间中的 xmlns:x 和 xmlns:d 的作用。xmlns:x 主要用于XAML语言解析和编译,提供XAML编译指令,如x:Class用于指定与C#代码的结合。而xmlns:d 则是设计视图相关的属性,仅在设计时有效,运行时被忽略,用于支持Visual Studio和Expression Blend的设计功能。

725

被折叠的 条评论
为什么被折叠?



