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个传统形式的命名空间,融合成一个网址,节约手工代码量。其核心目的是将同一个公司的命名空间,使用惟一的引用方式引入。