[XAML]命名空间xmlns

在编写WPF的XAML文件时,我们都会看到系统生成的默认文件里,包含如下东西:

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

 那么他们是什么意思呢?

xmlns是XAML引用的命名空间

不带冒号的,是作为默认命名空间,例如:

<TextBlock />

 带冒号的,在XML里就需要用前缀来引用,例如:

<x:Code />

 当你复制上面引用的地址到浏览器里打开时,是访问不到的。

它们并不是真正存在于网络上,而是存在于程序集里。

用Reflector反射PresentationFramework程序集

注意看程序集的信息,里面有一堆这样的代码:

[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Controls")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Documents")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Shapes")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Shell")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Navigation")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Data")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Controls.Primitives")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Media.Animation")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Input")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Media")]

这些代码的作用就是把那些命名空间合并到一个统一的命名空间里,方便调用。

我们在自己的程序集里也可以这样用,只要修改AssemblyInfo.cs就可以了。

然后我们在XAML里添加命名空间,就会看到自己定义的命名空间了。

posted @ 2013-05-02 13:36  Kation  阅读(1427)  评论(0编辑  收藏  举报