Establishing WPF-specific XML Namespaces

The root element of a XAML document typically defines two XML namespaces that map to core WPF
namespaces and XAML specific tokens.
  •       
 http://schemas.microsoft.com/winfx/2006/xaml/presentation maps a number of WPF
  
          namespaces for use by the current *.xaml file:
          •        System.Windows, System.Windows.Controls, System.Windows.Data, System.Windows.Ink,
         
          System.Windows.Media, System.Windows.Navigation, 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 <Window> that defines these two XML namespaces. Given the cascading nature of XML,
all sub-elements of the
<Window> have access to the same information.


<Window
 xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml">

</Window>



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
<Window> definition is also permissible although more verbose.


<Window
 xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:
XamlSpecificStuff =
   "http://schemas.microsoft.com/winfx/2006/xaml">
</Window>



If you did use the ' XamlSpecificStuff' prefix, any XAML keyword in the defining scope would now need to
make use of this rather verbose tag. Consider the following
<Application> root element, which illustrates the
simplicity of
‘x’ as a tag prefix for the XAML-centric namespace.


<Application XamlSpecificStuff:Class="SimpleXamlApp.MyApp"
 xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:
XamlSpecificStuff = "http://schemas.microsoft.com/winfx/2006/xaml"
 StartupUri = "MainWindow.xaml" Exit="AppExit">

 <
XamlSpecificStuff:Code>
 <![CDATA[
 private void AppExit(object sender, ExitEventArgs e)
 {
   MessageBox.Show("App has exited");
 }
 ]]>
 </
XamlSpecificStuff:Code>

</Application>



Beyond the two key XML namespaces, XAML makes it possible to define custom xmlns values that map to
custom assemblies. This can be helpful when your markup needs to refer to types defined in external 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 a simple example that makes
the types in the
System namespace of mscorlib.dll available within the current <Window>. If you are
mapping to a namespace in the current assembly, the
assembly qualifier is optional as seen in the second
XML namespace listing.


<Window
 xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"

 xmlns:CorLib = "clr-namespace:System;assembly=mscorlib"
 xmlns:MyTypes = "clr-namespace:SomeNamespaceInMyAssembly">

</Window>
Establishing WPF-specific 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