An element name in XAML is a name in an XML namespace, and the namespace is what tells the reader which types to look in. The declaration is the XML one, xmlns for the default namespace and xmlns:prefix for a prefix. A document usually declares the framework as the default, the XAML language as x, and one prefix for each .NET namespace of the application that it uses.
The declaration and its scope
xmlns="URI"sets the namespace of every element with no prefix, in the element that holds the declaration and everything inside it.xmlns:name="URI"declares the prefix name;name:Elementis then an element of that namespace.- A declaration holds for the element and its descendants, and an inner one hides an outer one. Declare namespaces on the root unless a file merges parts written elsewhere.
- The default namespace does not apply to attributes. An attribute with no prefix is in no namespace and names a property or event of the element’s type; an attribute with a prefix (
x:Name,local:Tools.Tag,mc:Ignorable) is in that namespace. - A prefix that is used and not declared is an error (the XML reader reports it as an unbound prefix). A declaration that is never used is legal.
- Prefix names are free; the ones in the table below are the conventions the project templates write. The
xmlandxmlnsprefixes are reserved.
How a namespace becomes types
The URI is a name, not an address, and nothing is fetched. A framework that reads XAML keeps a table from namespace URIs to .NET namespaces in assemblies. There are two ways a URI gets into the table.
Assembly attributes: XmlnsDefinition and XmlnsPrefix
An assembly can state that a URI stands for one or more of its CLR namespaces with [assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Controls")]. Several attributes, in several assemblies, may name the same URI, and then the URI covers all of those CLR namespaces: this is how one URI, the presentation namespace, covers System.Windows, System.Windows.Controls, System.Windows.Media, System.Windows.Shapes, System.Windows.Documents and the rest of WPF. A third party library does the same to give its controls one URI (the table lists a few). XmlnsPrefix suggests the prefix a tool should write for it. XmlnsCompatibleWith says that a URI is an older name of another. Avalonia has an XmlnsDefinition attribute of the same name (in Avalonia.Metadata), and its own assemblies use it to map https://github.com/avaloniaui to Avalonia, Avalonia.Controls, Avalonia.Media and others. .NET MAUI maps its default namespace the same way. The attributes of WinUI were not checked.
CLR namespace mappings
The other way is to write the CLR namespace in the declaration, so that no attribute is needed.
| Form | Used by | Meaning |
|---|---|---|
clr-namespace:Namespace | WPF, .NET MAUI, Avalonia (Silverlight, Xamarin.Forms and Workflow Foundation also use it; not checked in a source) | the CLR namespace, looked up in the assembly the markup is compiled into |
clr-namespace:Namespace;assembly=AssemblyName | the same | the CLR namespace in the named assembly: its simple name, without .dll and without version, culture or key; required for a type that is in another assembly in loose XAML, optional in compiled XAML for the project’s own assembly |
using:Namespace | UWP, WinUI, .NET MAUI, Avalonia (Uno: not checked) | the CLR namespace; there is no assembly part. The UWP documentation writes it this way, and the Avalonia documentation calls it the recommended form |
Only the forms above are mapping syntax. A namespace URI that starts with clr-namespace but has a stray part (clr-namespace:A;B), no namespace name, or an empty assembly= is a mistake, and the viewer reports it. A generic type is not written in the mapping but in the element: <local:Box x:TypeArguments="sys:Int32"/> (language namespace).
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:models="clr-namespace:Demo.Models;assembly=Demo"
xmlns:vm="clr-namespace:Demo.ViewModels">
<UserControl.Resources>
<sys:String x:Key="Caption">Customers</sys:String>
<DataTemplate DataType="{x:Type models:Customer}">…</DataTemplate>
</UserControl.Resources>
</UserControl>
mscorlib in .NET Framework; in modern .NET its types live in System.Private.CoreLib and are exposed by System.Runtime.Markup compatibility and design-time namespaces
The Markup Compatibility namespace http://schemas.openxmlformats.org/markup-compatibility/2006 (prefix mc) comes from Office Open XML (ECMA-376). The XAML object mapping specification lets a reader apply it as a first step (preprocessed) or read the markup as it stands (raw). Its attributes and elements:
mc:Ignorable="prefix1 prefix2"on an element lists namespace prefixes whose elements and attributes a reader that does not understand them must skip. A prefix named here must be declared (the viewer reports one that is not).mc:ProcessContent="Element1 Element2"says that the content of an ignored element is still read.mc:MustUnderstand="prefix"says that a reader that does not understand the namespace must fail.mc:AlternateContentwithmc:Choice Requires="prefix"andmc:Fallbackpicks the first choice whose required namespaces the reader understands.
The design-time namespace http://schemas.microsoft.com/expression/blend/2008 (prefix d), listed in mc:Ignorable="d", holds attributes that only designers read: d:DesignWidth, d:DesignHeight, d:DataContext, d:DesignSource and the markup extensions d:DesignInstance and d:DesignData, which give them design-time data. The UWP documentation lists these. A running application skips them. Designers write other d: attributes too; they are not described here. MAUI and Xamarin.Forms have design-time namespaces of their own (http://schemas.microsoft.com/dotnet/2021/maui/design and http://xamarin.com/schemas/2014/forms/design) that are used the same way. Avalonia’s designer reads the d: namespace of Blend.
The well-known namespaces
| URI | Usual prefix | What it is |
|---|---|---|
http://schemas.microsoft.com/winfx/2006/xaml/presentation | (default) | the framework types of WPF, UWP and WinUI (the viewer assumes that Silverlight shares it; not checked in a source) |
http://schemas.microsoft.com/winfx/2006/xaml | x | the XAML language (the x: of WPF, UWP, WinUI and Avalonia). The MS-XAML 2006 and MS-XAML-2009 specifications both give this URI as the target namespace of their x: schema |
http://schemas.microsoft.com/winfx/2009/xaml | x | the x: that the .NET MAUI documentation writes; the MAUI documentation says it is the 2009 XAML specification. The WPF and UWP documentation do not use it, and the System.Xaml source lists only the 2006 URI as the XAML language namespace |
http://schemas.microsoft.com/client/2007 | (default) | a Silverlight core namespace of 2007 (not checked in a source) |
http://schemas.microsoft.com/expression/blend/2008 | d | design-time attributes |
http://schemas.openxmlformats.org/markup-compatibility/2006 | mc | markup compatibility |
http://schemas.microsoft.com/winfx/2006/xaml/composite-font | (default) | composite fonts |
https://github.com/avaloniaui | (default) | Avalonia’s controls, in .axaml |
http://schemas.microsoft.com/dotnet/2021/maui | (default) | .NET MAUI |
http://xamarin.com/schemas/2014/forms | (default) | Xamarin.Forms |
http://schemas.microsoft.com/netfx/2009/xaml/activities | (default) | Workflow Foundation 4 activities |
http://schemas.microsoft.com/netfx/2009/xaml/servicemodel | p or sad | WCF messaging activities (Receive, Send) |
http://schemas.microsoft.com/winfx/2006/xaml/workflow | (default) | Workflow Foundation 3 (.xoml) |
http://schemas.microsoft.com/build/2009/properties | (default) | the property page rules of the Visual Studio project system |
http://schemas.microsoft.com/xaml/behaviors | i or behaviors | XAML Behaviors (Microsoft.Xaml.Behaviors) |
http://schemas.microsoft.com/expression/2010/interactivity | i | the older Blend behaviours |
http://xamarin.com/schemas/2020/toolkit, http://schemas.microsoft.com/dotnet/2022/maui/toolkit | xct, toolkit | the Xamarin and MAUI community toolkits |
http://metro.mahapps.com/winfx/xaml/controls | mah or controls | MahApps.Metro |
http://materialdesigninxaml.net/winfx/xaml/themes | materialDesign | Material Design In XAML |
http://prismlibrary.com/ | prism | Prism |
http://schemas.xceed.com/wpf/xaml/toolkit | xctk | Extended WPF Toolkit |
http://schemas.telerik.com/2008/xaml/presentation | telerik | Telerik UI for WPF |
http://schemas.devexpress.com/winfx/2008/xaml/core | dx | DevExpress for WPF (one URI for each part of the suite) |
http://reactiveui.net | rxui | ReactiveUI |
The rows for the XAML language, the default namespaces of WPF, UWP, WinUI, Avalonia, .NET MAUI and Xamarin.Forms, d, mc, Workflow Foundation 4 activities, XAML Behaviors, MahApps.Metro, Material Design In XAML, Prism and the .NET MAUI Community Toolkit were checked against Microsoft documentation, source code or each project’s own source. The other rows (composite fonts, Workflow Foundation 3, the WCF messaging activities, the project system rules, the older Blend behaviours, the Xamarin Community Toolkit, Extended WPF Toolkit, Telerik, DevExpress and ReactiveUI) and every usual prefix other than x, d and mc were written from memory and are not verified.
The viewer’s Namespaces tab labels these when it finds them and shows any other URI as “not a mapping this viewer knows”, which is not a problem: the namespace of any library is a name its authors chose.
Not described
Known, not yet covered
- Reference: the list of third-party URIs is a sample of the common ones, not a register; there is no complete list.
- Reference: how each framework resolves the
using:andclr-namespace:forms (which assemblies it searches) was not tested; the page states the forms the documentation gives. - Reference:
XmlnsDefinitionand its relatives are described from the WPF documentation and the Avalonia source; the attributes of WinUI were not read. - Reference: which frameworks use
clr-namespace:beyond WPF, MAUI and Avalonia (Silverlight, Xamarin.Forms and Workflow Foundation are listed from memory), and whether Uno acceptsusing:.
Unknown
- Reference: from which .NET MAUI version
using:is accepted; the current MAUI documentation says it is.