XmlnsDefinition for a Cool Namespace Mapping
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!
最新文章
- linux 下安装 mysql5.7.16安装
- Performance Monitor1:开始性能监控
- Java实现中文数字转换为阿拉伯数字
- MySQL自动化安装(双主多从读写分离)
- Python程序的混淆和加密
- Java 连接SQLite数据库
- nginx和apache的优缺点比较
- 对Kalman(卡尔曼)滤波器的理解
- 使用Advanced Installer制作IIS安装包(一:配置IIS和Web.config)
- OS模块学习
- VS code的疑惑之处
- Golang的排序和查找
- 每天CSS学习之text-decoration
- flask_mysql入库
- 【Python】插入sqlite数据库
- Nginx配置项优化详解(转)
- cf623A. Graph and String(二分图 构造)
- 【leetcode 简单】 第九十五题 数字转换为十六进制数
- OGNL入门
- WebService第二天——WebService框架CXF
热门文章
- 绑定sql server数据库的用户与登录名
- IOS 程序员开发最常用宏定义
- Git 工具 - 凭证存储
- 期权、RSU的区别与行权事宜
- mysql中tinyint、smallint、int、bigint的区别
- 每日英语:China Destroys Six Tons of Confiscated Ivory
- 【Java Concurrency】sleep()、wait()、notify()、notifyAll()的用法与区别
- 笔记本貌似好了(HP 450 卡)
- Unique constraint on single String column with GreenDao2
- 解决opencv和mfc同时使用导致memory leak