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開頭,xmlns是XML 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項目中建立ClassTest和min項目中建立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個傳統形式的命名空間,融合成一個網址,節約手工代碼量。其核心目的是將同一個公司的命名空間,使用唯一的引用方式引入。
