Code Highlighter

Saturday 14 April 2012

WPF - Namespace One-to-Many

You develop WPF applications, you may realize that there are 3 types of namespace as shown below.

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:sys="clr-namespace:System;assembly=mscorlib"        
xmlns:mylib1="http://schemas.mycorp.com/mylibrary/2012" 

This blog, I will only discuss about the namespace of type one-to-many and its purpose.
xmlns:mylib1="http://schemas.mycorp.com/mylibrary/2012" 
You develop a WPF library and in your library has many namespaces, if you decide refactoring your library namespace. Off course,you do not want all your client applications have to change their XAML codes. This is the main reason why you should provide one-to-many namespace.

How to do it ?

Example : you have a WPF library that has 3 namespaces : 
  • MyWPFLib.Commands
  • MyWPFLib.Controls
  • MyWPFLib.Pages

All you have to do, it's to add the namespace definitions to the AssemblyInfo.cs
[assembly: XmlnsPrefix("http://schemas.mycorp.com/mylibrary/2012", "mylib")]
[assembly: XmlnsDefinition("http://schemas.mycorp.com/mylibrary/2012", "MyWPFLib.Commands")]
[assembly: XmlnsDefinition("http://schemas.mycorp.com/mylibrary/2012", "MyWPFLib.Controls")]
[assembly: XmlnsDefinition("http://schemas.mycorp.com/mylibrary/2012", "MyWPFLib.Pages")]

You can download the example here.
http://www.4shared.com/zip/ZwX8Wc-x/NameSpace.html