In XAML, when you want to reference a CLR type, you have to add a namespace mapping that maps the XML namespace to the CLR namespace, like so:

xmlns:local="clr-namespace:MyTestApp.Controls;assembly=TestApp.Controls"

However the Types from PresentationFramework and PresentationCore are included differently, like so:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

What I like about this declaration is the fact that it hides all the CLR-namespaces that are automatically imported. This is possible because inside PresentationFramework.dll (and some in PresentationCore.dll), we have a bunch of assembly attributes, namely the XmlnsDefinitionAttribute:

[assembly:
XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation",
"System.Windows")] [assembly:
XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation",
"System.Windows.Data")] [assembly:
XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation",
"System.Windows.Navigation")] [assembly:
XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation",
"System.Windows.Shapes")] [assembly:
XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation",
"System.Windows.Documents")] [assembly:
XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation",
"System.Windows.Controls")]

This does the mapping from the CLR namespace to the XML namespace. So instead of having a list of xmlns mappings in your XAML file, you just have one, of the formxmlns:x=“http://schemas.microsoft.com/winfx/2006/xaml/presentation. This not only simplifies the mapping declarations but it is also easier to remember. So say for example your company created a bunch of cool WPF controls; you could then have a XmlnsDefinitionAttribute in the Control Library as:

[assembly: XmlnsDefinition("http://www.my-company.com/wpf",
"MyCompany.Controls")]

Then your users could use these controls in their XAML files simply by including the namespace mapping:

xmlns:mc="http://www.my-company.com/wpf"

Now isn’t that a cooler way of declaring the XML namespace!

最新文章

  1. linux 下安装 mysql5.7.16安装
  2. Performance Monitor1:开始性能监控
  3. Java实现中文数字转换为阿拉伯数字
  4. MySQL自动化安装(双主多从读写分离)
  5. Python程序的混淆和加密
  6. Java 连接SQLite数据库
  7. nginx和apache的优缺点比较
  8. 对Kalman(卡尔曼)滤波器的理解
  9. 使用Advanced Installer制作IIS安装包(一:配置IIS和Web.config)
  10. OS模块学习
  11. VS code的疑惑之处
  12. Golang的排序和查找
  13. 每天CSS学习之text-decoration
  14. flask_mysql入库
  15. 【Python】插入sqlite数据库
  16. Nginx配置项优化详解(转)
  17. cf623A. Graph and String(二分图 构造)
  18. 【leetcode 简单】 第九十五题 数字转换为十六进制数
  19. OGNL入门
  20. WebService第二天——WebService框架CXF

热门文章

  1. 绑定sql server数据库的用户与登录名
  2. IOS 程序员开发最常用宏定义
  3. Git 工具 - 凭证存储
  4. 期权、RSU的区别与行权事宜
  5. mysql中tinyint、smallint、int、bigint的区别
  6. 每日英语:China Destroys Six Tons of Confiscated Ivory
  7. 【Java Concurrency】sleep()、wait()、notify()、notifyAll()的用法与区别
  8. 笔记本貌似好了(HP 450 卡)
  9. Unique constraint on single String column with GreenDao2
  10. 解决opencv和mfc同时使用导致memory leak