Creating Consolidated Namespaces

In Chapter 2, we briefly discussed the concept of consolidated namespaces, where multiple namespaces could be combined into a single namespace declaration. This enables you to use a single namespace prefix covering multiple namespaces and/or assemblies, instead of declaring separate prefixes for each namespace/assembly combination individually. You can recognize a consolidated namespace, as it has a URI namespace instead of a CLR namespace. The default, and XAML namespaces that you find declared in all XAML files, are examples of consolidated namespaces, as is the SDK namespace:

xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"

These consolidated namespaces are predefined for you, but it is entirely possible for you to create your own if you wish, using the XmlnsDefininition attribute, as long as there is no overlap between the class names in the namespaces that you are consolidating.

There are a number of reasons why you might like to do so. One is to reduce the number of namespace prefix declarations you need to add to each XAML file. Another is to ensure consistency of the prefixes defined for each namespace across XAML files—different developers on a project using different prefixes will make maintaining the application a painful chore. An additional advantage is that the developers don't need to know or worry about which namespace a particular class or control is in to use it.

Let's take a look at creating a consolidated namespace to consolidate a number of namespaces in your Silverlight project. In the AssemblyInfo.cs file in your project, which you can find under the Properties folder, start by adding a using directive to the System.Windows.Markup namespace. Now, you need to come up with a URI to refer to the consolidated namespace you are about to create. A rough standard for doing so is something along these lines:

"http://schemas.yourbusinessurl.com/XAML"

Now, simply add attributes to your assembly that each attach a CLR namespace to this consolidated namespace URI:

[assembly: XmlnsDefinition("http://schemas.yourbusinessurl.com/XAML",
                           "AdventureWorks.Purchasing")]
[assembly: XmlnsDefinition("http://schemas.yourbusinessurl.com/XAML",
                           "AdventureWorks.ViewModels.Production")]
[assembly: XmlnsDefinition("http://schemas.yourbusinessurl.com/XAML",
                           "AdventureWorks.ViewModels.Sales")]

Next, you can declare a namespace prefix in a XAML file to this consolidated namespace URI, and then all the classes or controls across the combined CLR namespaces will be accessible via that prefix:

xmlns:vm="http://schemas.yourbusinessurl.com/XAML"

If you wish, you can also define a default prefix for that consolidated namespace using the XmlnsPrefix attribute:

[assembly: XmlnsPrefix("http://schemas.yourbusinessurl.com/XAML", "vm")]

This prefix will be used by Visual Studio and Expression Blend as the consolidated namespace's prefix when a control under that namespace is dragged from the toolbox onto the designer surface.

images Note You will generally find this technique useful when creating custom control libraries, as discussed in Chapter 12, because you can define a consolidated namespace in a library that consolidates all the controls from different namespaces within that library into a single URI namespace.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset