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. In Avalonia, .NET MAUI and WinUI the attributes have the same names or close ones.
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, Silverlight, Xamarin.Forms, MAUI, Avalonia, Workflow Foundation | the CLR namespace, looked up in the assembly the markup is compiled into (and, in WPF, in the referenced assemblies by the markup compiler) |
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, Uno, Avalonia | the CLR namespace, searched in the project and its references; there is no assembly part; this is the form the UWP, WinUI and Avalonia project templates write |
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 Open Packaging and Office Open XML, and XAML readers apply it before they read anything else. 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; WPF uses it to write markup for newer versions that older ones can skip.
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:DesignInstance Type=vm:Main, IsDesignTimeCreatable=True}", d:DesignData, d:Source, d:Background, d:IsHidden, d:LayoutOverrides and d:IsDataSource. A running application skips them. 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, Silverlight 2 to 5, Windows Phone, UWP, WinUI |
http://schemas.microsoft.com/winfx/2006/xaml | x | the XAML language, 2006 |
http://schemas.microsoft.com/winfx/2009/xaml | x | the XAML language, 2009 (also the x: of Xamarin.Forms and MAUI) |
http://schemas.microsoft.com/client/2007 | (default) | Silverlight 1.0 and the 2007 core types |
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 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 and what the templates write. - Reference:
XmlnsDefinitionand its relatives are described from the WPF documentation; the equivalents of MAUI and Avalonia were not read in their source.
Unknown
- Reference: whether MAUI accepts
using:in every version.