原文:XAML的命名空间 - CSDN博客

一个最简单的XAML例子

  1. <Window x:Class="WpfApplication1.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="MainWindow" Height="350" Width="525">
  5. <Grid>
  6. </Grid>
  7. </Window>
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid> </Grid>
</Window>
  1. xmlns特征的语法格式如下:xmlns[:可选的映射前缀]="名称空间"
xmlns特征的语法格式如下:xmlns[:可选的映射前缀]="名称空间"

xmlns后可以跟一个可选的映射前缀,之间用冒号分隔,如果没有写可选映射前缀,就意味着所有来自这个名称空间的标签都不用加前缀,这个没有映射前缀的名称空间称为“默认名称空间”,默认名称空间只能有一个。上面的例子中,Window和Grid都属于 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation声明的默认命名空间,而Class特征来自于
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml申明的命名空间,如果给 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation申明的命名空间加上一个前缀,那么代码必须修改成这样

  1. <n:Window x:Class="WpfApplication1.MainWindow"
  2. xmlns:n="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="MainWindow" Height="350" Width="525">
  5. <n:Grid>
  6. </n:Grid>
  7. </n:Window>
<n:Window x:Class="WpfApplication1.MainWindow"
xmlns:n="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<n:Grid> </n:Grid>
</n:Window>
 
  1. 在C#中,如果要使用System.Windows.Controls名称空间里的Button类,需要先把包含System.Windows.Controls美林歌城空间的程序集PresentationFramework.dll通过添加引用的方式引用到项目中,然后再在C#代码的顶端输入using System.Windows.Controls;
在C#中,如果要使用System.Windows.Controls名称空间里的Button类,需要先把包含System.Windows.Controls美林歌城空间的程序集PresentationFramework.dll通过添加引用的方式引用到项目中,然后再在C#代码的顶端输入using System.Windows.Controls;
 
  1. 在XAML如果需要使用Button类,也需要先添加对程序集的引用,然后在根元素的起始标签中写上一句:xmlns:c="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"
在XAML如果需要使用Button类,也需要先添加对程序集的引用,然后在根元素的起始标签中写上一句:xmlns:c="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"
 
  1. x:Class这个属性的作用是当XAML解析器将包含它的标签解析成C#类后的类名,用Windows SDK自带的工具IL反汇编程序对编译出来的exe进行反汇编,可以发现生成了MainWindow类
 x:Class这个属性的作用是当XAML解析器将包含它的标签解析成C#类后的类名,用Windows SDK自带的工具IL反汇编程序对编译出来的exe进行反汇编,可以发现生成了MainWindow类
 
 
 
在XAML中引用名称空间的语法是:

  1. xmlns:映射名="clr-namespace:类库中名称空间的名字;assembly=类库文件名"
xmlns:映射名="clr-namespace:类库中名称空间的名字;assembly=类库文件名"


例如:MyLibrary.dll中包含Common和Control两个名称空间,而且已经把这个程序集引用进WPF项目,那么在XAML中对于这两个名称空间,XAML中的引用会是:
  1. xmlns:common="clr-namespace:Common;assembly=MyLibrary"
  2. xmlns;control="clr=namespace:Control;assembly=MyLibrary"
xmlns:common="clr-namespace:Common;assembly=MyLibrary"
xmlns;control="clr=namespace:Control;assembly=MyLibrary"


最新文章

  1. Linux下man手册使用
  2. MySQL 中的 FOUND_ROWS() 与 ROW_COUNT() 函数
  3. 2014江西理工大学C语言程序竞赛高级组
  4. [ Office 365 开发系列 ] 开发模式分析
  5. Java带包编译运行
  6. C#与java中的集合区别
  7. App_Code
  8. java.lang.SecurityException:Invalid signature file digest forManifest main attributes
  9. [笔记]我的Linux入门之路 - 03.Java环境搭建
  10. 进程控制fork与vfork
  11. Codeforces 803C. Maximal GCD
  12. @Autowired 与@Resource的区别(详细)
  13. centos7通过yum安装mysql,并授权远程连接 【转】
  14. VUE 前端项目优化方法
  15. Ubuntu忘记密码的解决办法
  16. [转载]java中io流关闭的顺序
  17. Unity打安卓包 Android 所有错误解决方案大全(几乎囊括所有打包错误 )
  18. poj1151 Atlantis &amp;&amp; cdoj 1600艾尔大停电 矩形面积并
  19. 流程控制(if、while、for)
  20. xcode没有ios7的模拟器

热门文章

  1. php实现字符串的排列(交换)(递归考虑所有情况)
  2. [RxJS] Use groupBy in real RxJS applications
  3. 不使用left-join等多表关联查询,只用单表查询和Java程序,简便实现“多表查询”效果
  4. 【codeforces 768A】Oath of the Night's Watch
  5. Eclipse Che安装入门和使用(一)
  6. 小强的HTML5移动开发之路(26)—— JavaScript回顾1
  7. C++程序员经常问的11个问题
  8. hexo改造
  9. Android菜鸟的成长笔记(16)——Service简介
  10. 基于Geoserver发布时间地图