Why are there XAML namespaces that are URLS?

后端 未结 2 468
情话喂你
情话喂你 2020-12-09 10:30

In Silverlight/WPF xaml at the top of the code you have your namespace/import type declarations. I can easily understand how these declarations can point to an assembly so t

2条回答
  •  攒了一身酷
    2020-12-09 11:11

    There's an attribute you can use in the code of your referenced assembly that maps a Uri to your code namespaces:

    [XmlnsDefinitionAttribute("http://yournamespace/", "Your.Assembly.Namespace")]
    

    You can include multiple of these attributes, typically in your AssemblyInfo.cs, allowing multiple code namespaces to be referenced by a single Uri namespace in Xaml.

    This makes your namespace declarations more compact (since you can omit the assembly name). It also allows you some flexibility in reorganizing namespaces in the referenced assembly without breaking your markup.

    EDIT: for example, if you point Reflector at the PresentationCore assembly, you can see attributes such as this at the assembly level:

    [assembly: 
        XmlnsDefinition( "http://schemas.microsoft.com/netfx/2007/xaml/presentation"
                       , "System.Windows.Ink") ]
    

    This is how the Uri import gets mapped to code namespaces.

提交回复
热议问题