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!

XmlnsDefinition for a Cool Namespace Mapping的更多相关文章

  1. 菜鸟学Struts2——Struts工作原理

    在完成Struts2的HelloWorld后,对Struts2的工作原理进行学习.Struts2框架可以按照模块来划分为Servlet Filters,Struts核心模块,拦截器和用户实现部分,其中 ...

  2. Phoenix综述(史上最全Phoenix中文文档)

    个人主页:http://www.linbingdong.com 简书地址:http://www.jianshu.com/users/6cb45a00b49c/latest_articles 网上关于P ...

  3. Struts2 源码分析——Result类实例

    本章简言 上一章笔者讲到关于DefaultActionInvocation类执行action的相关知识.我们清楚的知道在执行action类实例之后会相关处理返回的结果.而这章笔者将对处理结果相关的内容 ...

  4. Struts2 源码分析——Action代理类的工作

    章节简言 上一章笔者讲到关于如何加载配置文件里面的package元素节点信息.相信读者到这里心里面对struts2在启动的时候加载相关的信息有了一定的了解和认识.而本章将讲到关于struts2启动成功 ...

  5. Struts2 源码分析——调结者(Dispatcher)之执行action

    章节简言 上一章笔者写关于Dispatcher类如何处理接受来的request请求.当然读者们也知道他并非正真的执行action操作.他只是在执行action操作之前的准备工作.那么谁才是正真的执行a ...

  6. 浅谈:深入理解struts2的流程已经spring和struts2的整合

    第一步:在tomcat启动的时候 1.在tomcat启动的时候,首先会加载struts2的核心过滤器StrutsPrepareAndExecuteFilter <filter> <f ...

  7. Struts2与Ajax的整合

    整合: 导入jar包 sturts2-json-plugin-2.1.8.1.jar 说明: 在该jar包中有struts-plugin.xml文件 <struts>            ...

  8. Struts2.3.15.1源码浅析

    Struts2 两大运行主线: 1.初始化主线:初始化主线主要是为Struts2创建运行环境(此处的环境与Struts2身处的Web环境是有区别的),初始化入口StrutsPrepareAndExec ...

  9. WS+MQ+WCF+EF(Code First)

    前言 有段时间没有更新博文了,一直在忙工作很少有时间静下心来继续研究点东西,说来也惭愧,归咎原因最主要的还是因为懒惰.空想也是不管用的,有时候很多想法被扼杀到了摇篮里,还没开始做就放弃了,这是多数人会 ...

随机推荐

  1. Android Studio 1.1.0 “关联源码” 或者“导入源码” ,又或者插件包

    其实这博文是废话!为什么呢? 1.如果自己的SDK没有更新相应当前操作版本的source的话,相应的v4,v7等等的源码都不会自动导入的. 其实Android Studio自身就已经会去检测你当前SD ...

  2. 【小白的CFD之旅】21 网格划分软件的选择

    但是怎样才能获得流体计算网格呢?“工欲善其事必先利其器”,画网格该用什么器呢?小白决定找黄师姐请教一番. 小白找到黄师姐的时候,黄师姐正在电脑上忙着. “黄师姐,我发现网格划分软件有好多种,究竟哪种才 ...

  3. TimescaleDB比拼InfluxDB:如何选择合适的时序数据库?

    https://www.itcodemonkey.com/article/9339.html 时序数据已用于越来越多的应用中,包括物联网.DevOps.金融.零售.物流.石油天然气.制造业.汽车.太空 ...

  4. IBM研究院找到度量安全性方法:容器与虚拟机,谁更安全?

    https://zhuanlan.zhihu.com/p/40446759 虚拟机比容器更安全吗?你可能会有自己的答案,但IBM研究院发现容器的安全性与虚拟机一样,甚至更加安全. 一般来说,从接口宽度 ...

  5. vscode 换行符\n 变成\r\n

    VSCode是一个开源的强大代码编写器,但是如果没有好好的配置使用,会适得其反. 这里总结VSCode的一些配置,方便自己查询,也方便网友. 1.编辑器配置 为特定类型文件指定缩进大小.缩进类型(空格 ...

  6. C++11 constexpr使用

    C++11为了提高代码执行效率做了一些改善.这种改善之一就是:生成常量表达式,允许程序利用编译时的计算能力.假如你熟悉模板元编程,你将发现constexpr使这一切变得更加简单.constexpr使我 ...

  7. Asp.Net导出文件名中文乱码

    Asp.Net导出word为例,Excel等其他文件也一样 protected void Page_Load(object sender, EventArgs e) {string html = “网 ...

  8. 2015-2016款Mac安装win10多分区教程,不破坏GUID分区表。

    原文:https://bbs.feng.com/read-htm-tid-10895240.html 参考:https://bbs.feng.com/read-htm-tid-9940193.html ...

  9. LVS之NAT和DR服务脚本

    NAT服务控制脚本 #!/bin/bash # # chkconfig: - 88 12 # description: LVS script for VS/NAT # . /etc/rc.d/init ...

  10. Facebook的Fairseq模型详解(Convolutional Sequence to Sequence Learning)

    1. 前言 近年来,NLP领域发展迅速,而机器翻译是其中比较成功的一个应用,自从2016年谷歌宣布新一代谷歌翻译系统上线,神经机器翻译(NMT,neural machine translation)就 ...