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. 关于Java Webproject中web.xml文件

    提及Java Webproject中web.xml文件无人不知,无人不识,呵呵呵:系统首页.servlet.filter.listener和设置session过期时限.张口就来,但是你见过该文件里的e ...

  2. 使用MySQL Proxy和MySQL Replication实现读写分离

    MySQL Replication可以将master的数据复制分布到多个slave上,然后可以利用slave来分担master的读压力.那么对于前台应用来说,就要考虑如何将读的压力分布到多个slave ...

  3. Logcat + money 笔记

    如下命令:将过滤后的日志按照指定格式输出到指定的文件中 adb logcat -v time -s Test_Tag:v > logcat_local.txt A:其中 -v time 用来指定 ...

  4. InfoQ 趋势报告:架构和设计领域技术演变详解

    https://www.infoq.cn/article/R7lWXd0R4VFf3E0bB*38 本文概述了我们对当前“架构和设计”领域的看法,这个领域侧重于基础设施模式.技术框架模式的实现,以及软 ...

  5. Mac终端中主机名与用户名的修改

    Mac终端的主机名称+用户名全部显示出来的话会可能显得特别长,影响整体美观,可以切换至root用户后 修改.bashrc文件进行格式化. 1. 终端中切换root用户 su - root 2. 修改/ ...

  6. 【Java工具方法】给集合按数量分组

    有时候需要给集合(如List)按数量分组,比如全集太大时,需要分批处理:或效率有点低,分批并发处理.于是,写了个将List按数量分组的方法. package controller; import ja ...

  7. Effective Spark RDDs with Alluxio【转】

    转自:http://kaimingwan.com/post/alluxio/effective-spark-rdds-with-alluxio 1. 介绍 2. 引言 3. Alluxio and S ...

  8. Asp.Net MVC绑定DropDownList等控件

    测试环境:vs2013..Net4.5.mvc5 一.Asp.Net MVC绑定控件原理说明 以Html.TextBox为例 /// <param name="name"&g ...

  9. Java自动创建多层文件目录

    // 创建文件上传路径 public static void mkdir(String path) { File fd = null; try { fd = new File(path); if (! ...

  10. Android 底部菜单固定到底部

    今天搞了很久的一个问题,导航菜单没有固定到底部,因为上面是ListView,可是没内容,于是就浮动上去了. 效果如下: 这里采用的是一个碎片,代码是: <LinearLayout xmlns:a ...