When you need a visual element from anywhere other than the main Silverlight namespaces/assemblies, it can be a pain to register namespaces. If I want a control from the Silverlight toolkit System.Windows.Layout.Toolkit assembly:
1 | <span style="color:#606060" id="lnum1"> 1:</span> xmlns:layouttk="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Layout.Toolkit" |
These are a pain because either you wait for Intellisense to pop up once the cursor is inside the quotation marks (I’ve noticed some don’t even realize Intellisense works here as it can sometimes take so long to open) or you memorize the format. You can also use Resharper, but that defeats the purpose of what I’m about to discuss.
Once I have registered my namespace and namespace prefix, I can use the prefix to specify things in the associated assembly/namespace:
1 | <span style="color:#606060" id="lnum1"> 1:</span> <span style="color:#0000ff"><</span><span style="color:#800000">layouttk:LayoutTransformer</span><span style="color:#0000ff">></</span><span style="color:#800000">layouttk:LayoutTransformer</span><span style="color:#0000ff">></span> |
What if I wanted to register the System.Windows.Controls.Data.Toolkit assembly as well? Here comes another namespace!
1 | <span style="color:#606060" id="lnum1"> 1:</span> xmlns:layouttk="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Layout.Toolkit" |
1 | <span style="color:#606060" id="lnum2"> 2:</span> xmlns:datatk="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Toolkit" |
And so on, compounded with my own user controls, converters, etc, until I end up with tens of namespaces on all of my XAML files. Luckily Microsoft did think of this and provide us with a simple shortcut. To see it in action, start declaring a namespace but let Intellisense open:
What you see here are a number of XML namespaces defined that contain multiple .NET namespace and assembly combinations. With this one namespace declaration:
1 | <span style="color:#606060" id="lnum1"> 1:</span> xmlns:tk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" |
…I have access to everything in the Silverlight Toolkit. If you are using any of the Expression Blend SDK, there are some namespaces you can register for them as well:
These can be incredibly useful when dealing with Microsoft’s various extension libraries, but what about my libraries? I tend to write a lot of reusable converters, user controls, view models, etc. To enable this with my own code, I simply need to use the XmlnsDefinitionAttribute.
1 | <span style="color:#606060" id="lnum1"> 1:</span> [assembly: System.Windows.Markup.XmlnsDefinition( <span style="color:#006080">"http://foo.bar"</span>, <span style="color:#006080">"SilverlightApplication5.Controls"</span> )] |
1 | <span style="color:#606060" id="lnum2"> 2:</span> [assembly: System.Windows.Markup.XmlnsDefinition( <span style="color:#006080">"http://foo.bar"</span>, <span style="color:#006080">"SilverlightApplication5.Converters"</span> )] |
When I do this, I now have access to all my registered namespaces via the “http://foo.bar” XML namespace.
1 | <span style="color:#606060" id="lnum1"> 1:</span> xmlns:my="http://foo.bar" |
As I build up a library of shared code, I can easily reference my shared code in XAML via these defined XML namespaces and save myself the hassle of having to manually register individual assemblies and namespaces.