• 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!

  • 相关阅读:
    阅读INI档
    jQuery遍历table中间tr td并获得td价值
    PB控制性能TreeView
    [POJ 3311]Hie with the Pie——谈论TSP难题DP解决方法
    数据结构 线性表
    ORACLE11G RAC 施加以分离不同的实例.TAF
    一起学习android使用一个回调函数onCreateDialog实现负载对话(23)
    [cocos2d-x 3.0] 触摸显示器
    lua学习笔记10:lua简单的命令行
    Matlab图像处理系列4———傅立叶变换和反变换的图像
  • 原文地址:https://www.cnblogs.com/itelite/p/4092191.html
Copyright © 2020-2023  润新知