WPF命名空间

namespace"命名空间"VS.NET中的各种语言使用的一种代码组织的形式通过名称空间来分类,区别不同的代码功能,同时也是VS.NET中所有类的完全名称的一部分。

1、建立命名空间

我们创建一个默认的WPF程序,其会根据项目名称建立一个默认的命名空间

 

 1 <Window x:Class="Example.MainWindow"
 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6 xmlns:local="clr-namespace:Example"
 7 mc:Ignorable="d"
 8 Title="MainWindow" Height="350" Width="525">
 9 <Grid>
10 </Grid>
11 </Window>

 其中以下代码当前应用程序的代表着命名空间,其组成方式是以xmlns:local开头,xmlnsXML Namespaces的缩写,代表xml命名空间。local代表本地xml所有在的命名空间,也就是"别名",就是对当前xml进行命名空间限定。所有命名空间都需以clr-namespace作为前缀,代表着当前的clr的命名空间限定,我们理解的意思是:"xml的本地命名空间等于clr命名空间且为Example"。

xmlns:local="clr-namespace:Example" 

以下代码代表本类的类限定名,是命名空间+类名。

x:Class="Example.MainWindow"

 其对应的代码命名空间如下:

namespace Example 
{ 
public partial class MainWindow : Window
{ 
public MainWindow() 
{ 
InitializeComponent(); 
} 
} 
}

 2、引用命名空间

我们新建里一下项目com.albert.test,在当前项目引用com.albert.test项目,在xml定义如下:

<Window x:Class="Example.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"
xmlns:local="clr-namespace:Example"
xmlns:albert="clr-namespace:com.albert.test;assembly=com.albert.test"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
 
</Grid>
</Window>

其指定的xmlns方式为xmlns:albert="clr-namespace:com.albert.test;assembly=com.albert.test",其意思根据命名空间的定义,很容易读懂,albert代表命名空间的别名,assembly代表当前引用的程序集的命名,其对应这com.albert.test中的AssemblyInfo.cs文件中的描述。

代码中引入命名空间的方式和常规方式相似。

3、引入网址的命名空间

查看以上代码,我们发现,很多命名空间是以http协议的方式呈现,这种命名空间的目的是什么呢?用.NetReflector反编译WindowsBase.dll,可以看到,一个网址对应多个命名空间。

这个与传统的一个命名空间对应一个程序集的方式不太一样,我们定义一个自己的网址命名空间可以如下操作。

在之前新建的com.albert.test项目中的AssemblyInfo.cs增加下面一行

[assembly: XmlnsDefinition("http://www.albert.com", "com.albert.test")]

再回到主程序,我们可以引用如下命名空间

xmlns:local="clr-namespace:Example"
xmlns:albert="http://www.albert.com"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">

我们将一个程序集的命名控制,转变为网址的命名空间,同时我们添加项目com.albert.min项目,也添加网址定义,如下:

[assembly: XmlnsDefinition("http://www.albert.com", "com.albert.min")]

分别在test项目中建立ClassTestmin项目中建立ClassMin,则直接引用http://www.albert.com地址命名空间,则可以同时引用两个命名空间。其效果如下:

<Window x:Class="Example.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"
xmlns:local="clr-namespace:Example"
xmlns:albert="http://www.albert.com"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<albert:ClassTest></albert:ClassTest>
<albert:ClassMin></albert:ClassMin>
</Grid>
</Window>

由此可见,网址形式的命名空间等于将原来N个传统形式的命名空间,融合成一个网址,节约手工代码量。其核心目的是将同一个公司的命名空间,使用唯一的引用方式引入。

 

posted @ 2017-08-28 10:40  Min.Xiaoshuang  阅读(4816)  评论(0编辑  收藏  举报