Establishing XML Namespaces

The root element of a XAML document typically defines two XML namespaces that map to core Silverlight .NET
namespaces and XAML specific tokens.
   •       
 http://schemas.microsoft.com/winfx/2006/xaml/presentation maps a number of .NET
            namespaces for use by the current *.xaml file:
           •        System.Windows, System.Windows.Controls, System.Windows.Data, System.Windows.
                    Media, System.Windows.Shapes, etc.
   •        
http://schemas.microsoft.com/winfx/2006/xaml is used to include XAML specific tokens, as
            well as a subset of types within the
System.Windows.Markup namespace.

Here would be a <UserControl> that defines these two XML namespaces. Given the cascading nature of XML, all
sub-elements of the
<UserControl> have access to the same information.


<UserControl x:Class = "SimpleSilverlightApplication.Page"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml">

<!-- Add content here! -->

</UserControl>


The first xmlns attribute is the primary namespace as it has not been qualified with a namespace prefix. Notice that
the second xmlns attribute as been given the
‘x’ prefix. This is simply to avoid ambiguity with the other namespace
definitions. Like any XML prefix, the actual name is irrelevant. Thus, the following <UserControl> definition is
also permissible although more verbose.


<UserControl XamlSpecificStuff:Class = "SimpleSilverlightApplication.Page"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:
XamlSpecificStuff = "http://schemas.microsoft.com/winfx/2006/xaml">

</UserControl>


Beyond the two key XML namespaces, XAML makes it possible to define custom xmlns values that map to
custom assemblies / namespaces. This can be helpful when your markup needs to refer to types defined in external
libraries. This is very common when your Silverlight application needs to access custom controls packaged up in .
NET class libraries. The
clr-namespace token is used to do this very thing.

You will see why this can be quite useful over the course of this class. Here is an example that makes the types in
the
System namespace of mscorlib.dll available within the current <UserControl>. If you are mapping to a
namespace in the current Silverlight project, the
assembly qualifier is optional as seen in the second XML
namespace listing.


<UserControl x:Class = "SimpleSilverlightApplication.Page"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:SystemCorLib = "clr-namespace:System;assembly=mscorlib"
xmlns:MyTypes = "clr-namespace:SomeNamespaceInMyAssembly">

</UserControl>
Establishing XML Namespaces
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Services