XAML Namespaces and Namespace Mapping

This topic further explains the presence and purpose of the two namespace mappings as found in the root tag of each Extensible Application Markup Language (XAML) file. It also describes how to produce similar mappings for using elements that are defined in your own code, and/or within separate assemblies.

This topic contains the following sections.

  • The WPF and XAML Namespace Declarations
  • Mapping To Custom Classes and Assemblies
  • Mapping CLR Namespaces to XML Namespaces in an Assembly
  • Related Topics

The WPF and XAML Namespace Declarations

Within the namespace declarations in the root tag of many Extensible Application Markup Language (XAML) files, you will see that there are two xmlns declarations. The first declaration maps the overall Windows Presentation Foundation (WPF) namespace as the default:

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

The second declaration maps a separate Extensible Application Markup Language (XAML) namespace, mapping it (typically) to the x: prefix.

xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"

The relationship between these declarations is that effectively XAML is a language standard, and WPF is one implementation that uses XAML as a language. The XAML language specifies certain language elements that are presumed to be implemented in order to be compliant, and each of these should be accessible through XAML reader or writer implementations working against the XAML namespace. The WPF implementation reserves the default namespace for its own APIs, and uses a separate mapped prefix for the markup syntax expected in XAML. By convention that prefix is x:, and this same x: convention is followed by project templates, sample code, and the documentation of language features within this SDK. The XAML namespace defines many commonly-used features that are necessary even for basic WPF applications. For instance, in order to join any code-behind to a XAML file through a partial class, you must name that class as the x:Class attribute in the root element of the relevant XAML file. Or, any element as defined in a XAML page that you wish to access as a keyed resource should have the x:Key attribute set on the element in question. For more information on these and other aspects of XAML see XAML Overview or XAML Syntax Terminology.

Mapping To Custom Classes and Assemblies

You can map namespaces to assemblies using a series of tokens within an xmlns prefix declaration, similar to how the standard WPF and XAML namespaces are mapped to prefixes.

The syntax takes the following possible named tokens and following values:

clr-namespace: The common language runtime (CLR) namespace declared within the assembly that contains the public types to expose as elements.

assembly= the assembly that contains some or all of the referenced CLR namespace. This value is typically just the name of the assembly, not the path. The path to that assembly must be established as a project reference in the project file that produces the compiled XAML. Optionally, in order to incorporate versioning and strong name signing, the value may instead be a string as defined by AssemblyName.

Note that the character separating the clr-namespace token from its value is a colon (:) whereas the character separating the assembly token from its value is an equals sign (=). The character to use between these two tokens is a semicolon. For example:

xmlns:custom="clr-namespace:SDKSample;assembly=SDKSampleLibrary"

Mapping to Current Assemblies

assembly can be omitted if the clr-namespace referenced is being defined within the same assembly as the application code that is referencing the custom classes. Or, an equivalent syntax for this case is to specify assembly=, with no string token following the equals sign.

Custom classes cannot be used as the root element of a page if defined in the same assembly. Partial classes don't need to be mapped, only classes that are not the partial class of a page in your application will need to be mapped if you intend to reference them as elements in XAML.

Mapping CLR Namespaces to XML Namespaces in an Assembly

WPF defines a CLR attribute that is consumed by XAML readers in order to map multiple CLR namespaces to a single XML namespace. This attribute, XmlnsDefinitionAttribute, is placed at the assembly level in the source code that produces the assembly. The WPF assembly source code uses this attribute to map the various common namespaces such as System.Windows, System.Windows.Controls, etc. to the https://schemas.microsoft.com/winfx/2006/xaml/presentation namespace.

The XmlnsDefinitionAttribute takes two parameters: the XML namespace name, and the CLR namespace name. More than one XmlnsDefinitionAttribute can exist to map multiple multiple CLR namespaces to the same XML namespace. Once mapped, members of those namespaces can also be referenced without full qualification if desired by providing the appropriate using statement in the partial class code-behind page. For more details, see XmlnsDefinitionAttribute.

See Also

Other Resources

Understanding XML Namespaces