namespace即"命名空間",VS.NET中的各類語言使用的一種代碼組織的形式經過名稱空間來分類,區別不一樣的代碼功能,同時也是VS.NET中全部類的徹底名稱的一部分。express
一、創建命名空間 spa
咱們建立一個默認的WPF程序,其會根據項目名稱創建一個默認的命名空間code
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"。orm
xmlns:local="clr-namespace:Example"
如下代碼表明本類的類限定名,是命名空間+類名。xml
x:Class="Example.MainWindow"
其對應的代碼命名空間以下:blog
namespace Example { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } } }
二、引用命名空間 it
咱們新建裏一下項目com.albert.test,在當前項目引用com.albert.test項目,在xml定義以下:io
<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文件中的描述。編譯
代碼中引入命名空間的方式和常規方式類似。form
三、引入網址的命名空間
查看以上代碼,咱們發現,不少命名空間是以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個傳統形式的命名空間,融合成一個網址,節約手工代碼量。其核心目的是將同一個公司的命名空間,使用惟一的引用方式引入。