Monday, January 12, 2009

How to assign a URL to one or more CLR namespaces

Look at the way the XML namespaces are defined in the snippet below. The 'local' definition is mapped directly to a CLR namespace in the normal way but the first two definitions are just URLs. What do thes URLs represent and where are they defined?
<Window x:Class="WpfLibrary.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfLibrary">
    
    <Window.Resources>
        <SolidColorBrush Color="AliceBlue"/>
    </Window.Resources>
    
    <TextBox/>
    
    
    
</Window>

The URLs have been setup to represent multiple CLR namespaces in the AssemblyInfo of the assembly in which they are defined. Notice that both the TextBox and SolidColorBrush classes are referenced using the default XML namespace even though the classes are actually defined in different CLR namespaces.

To find out which CLR namespaces are assigned to a particular URL then you can look at the assembly definition in Reflector.

Here's the syntax to set this up in the AssemblyInfo of your assembly:
[assembly:XmlnsDefinition("http://myorganization.com", "WpfLibrary")]
[assembly:XmlnsDefinition("http://myorganization.com", "WpfLibrary.Pens")]
[assembly:XmlnsDefinition("http://myorganization.com", "WpfLibrary.Brushes")]
Consumers of your assembly can then use the XML namespace to reference all of the associated CLR namespaces in one swoop:
<Window x:Class="WpfApplication1.Window1"
 xmlns:myorg="http://myorganization.com"
</Window>




No comments: