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 form xmlns: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!

Technorati tags: , , , ,

Similar Posts:

4 responses to “XmlnsDefinition for a cool namespace mapping”

  1. Pavan Podila

    Yes, I know thats a problem. You will have to resort to the other tactic, ie using clr-namespace inside your own project. I am thinking that since we are adding an assembly attribute, it will only be available when the assembly is built (ie in its DLL form). So its only the users of the library who can use the url syntax for namespace mapping.

  2. (no name)
    This attribute works for mapping namespaces from other assemblies, but fails when I try to use it from within the current assembly (that is, for importing controls that are defined in sub-namespaces within the same project). Is there a way to achieve the latter?
  3. Pavan Podila

    Thanks Sheva for pointing that out. Wonder how I missed it. May be I had an outdated blog feed.

  4. footballism

    hey, Pavan, ages ago, I’ve also posted something like this.
    http://shevaspace.spaces.live.com/blog/cns!FD9A0F1F8DD06954!420.entry

    Sheva

Leave a Reply