Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
XAML uses the xmlns XML attribute for namespace declarations. There are two standard XAML namespace declarations that typically appear within the root element of a XAML file. The first defines the default namespace:
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
The default namespace specifies that elements defined within the XAML file with no prefix refer to .NET Multi-platform App UI (.NET MAUI) classes, such as ContentPage, Label, and Button.
The second namespace declaration uses the x prefix:
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
XAML uses prefixes to declare non-default namespaces, with the prefix being used when referencing types within the namespace. The x namespace declaration specifies that elements defined within XAML with a prefix of x are used for elements and attributes that are intrinsic to XAML (specifically the 2009 XAML specification).
The following table outlines the x constructs supported by .NET MAUI:
| Construct | Description |
|---|---|
x:Arguments |
Specifies constructor arguments for a non-default constructor, or for a factory method object declaration. |
x:Class |
Specifies the namespace and class name for a class defined in XAML. The class name must match the class name of the code-behind file. Note that this construct can only appear in the root element of a XAML file. |
x:ClassModifier |
Specifies the access level for the generated class in the assembly. |
x:DataType |
Specifies the type of the object that the XAML element, and it's children, will bind to. |
x:FactoryMethod |
Specifies a factory method that can be used to initialize an object. |
x:FieldModifier |
Specifies the access level for generated fields for named XAML elements. |
x:Key |
Specifies a unique user-defined key for each resource in a ResourceDictionary. The key's value is used to retrieve the XAML resource, and is typically used as the argument for the StaticResource markup extension. |
x:Name |
Specifies a runtime object name for the XAML element. Setting x:Name is similar to declaring a variable in code. |
x:TypeArguments |
Specifies the generic type arguments to the constructor of a generic type. |
For more information about the x:ClassModifier attribute, see Class modifiers. For more information about the x:DataType attribute, see Compiled bindings. For more information about the x:FieldModifier attribute, see Field modifiers. For more information about the x:Arguments and x:FactoryMethod attributes, see Pass arguments. For more information about the x:TypeArguments attribute, see Generics.
Note
In addition to the constructs listed above, .NET MAUI also includes markup extensions that can be consumed through the x namespace prefix. For more information, see Consume XAML Markup Extensions.
In XAML, namespace declarations inherit from parent element to child element. Therefore, when defining a namespace in the root element of a XAML file, all elements within that file inherit the namespace declaration.
Implicit namespace declarations
Starting in .NET 11, implicit XAML namespace declarations are enabled by default. The compiler automatically injects the two standard namespace declarations, so you no longer need to include them in the root element of your XAML files:
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"— the default .NET MAUI namespace.xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"— the XAML language namespace providingx:Class,x:Name, and other intrinsic constructs.
This means a content page that previously required explicit namespace declarations:
<!-- .NET 10 and earlier: explicit namespace declarations required -->
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.MainPage">
<Label Text="Hello, World!" />
</ContentPage>
Can now be written without them:
<!-- .NET 11: standard namespaces are implicit -->
<ContentPage x:Class="MyApp.MainPage">
<Label Text="Hello, World!" />
</ContentPage>
Existing XAML files that include explicit declarations continue to compile without changes. Explicit declarations can also be used to disambiguate duplicate type names when needed.
Important
Custom namespaces still require explicit xmlns: declarations. Only the default .NET MAUI namespace and the x: XAML language namespace are implicitly available.
For example, you still need to declare a prefix for your own CLR namespaces or third-party libraries:
<ContentPage x:Class="MyApp.MainPage"
xmlns:local="clr-namespace:MyApp.Controls"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
<local:CustomControl />
</ContentPage>
In .NET 10, implicit namespace declarations are available as a preview feature. To opt in, add the following to your project file:
<PropertyGroup>
<DefineConstants>$(DefineConstants);MauiAllowImplicitXmlnsDeclaration</DefineConstants>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
</PropertyGroup>
When enabled, you can omit the standard xmlns and xmlns:x declarations from your XAML files. Custom namespaces still require explicit xmlns: declarations.
Declare namespaces for types
Types can be referenced in XAML by declaring a XAML namespace with a prefix, with the namespace declaration specifying the Common Language Runtime (CLR) namespace name, and optionally an assembly name. This is achieved by defining values for the following keywords within the namespace declaration:
clr-namespace:orusing:– the CLR namespace declared within the assembly that contains the types to expose as XAML elements. This keyword is required.assembly=– the assembly that contains the referenced CLR namespace. This value is the name of the assembly, without the file extension. The path to the assembly should be established as a reference in the project that contains the XAML file that will reference the assembly. This keyword can be omitted if the clr-namespace value is within the same assembly as the app code that's referencing the types.
Note
The character separating the clr-namespace or using token from its value is a colon, whereas the character separating the assembly token from its value is an equal sign. The character to use between the two tokens is a semicolon.
The following code example shows a XAML namespace declaration:
<ContentPage ... xmlns:local="clr-namespace:MyMauiApp">
...
</ContentPage>
Alternatively, this can be written as:
<ContentPage ... xmlns:local="using:MyMauiApp">
...
</ContentPage>
The local prefix is a convention used to indicate that the types within the namespace are local to the app. Alternatively, if the types are in a different assembly, the assembly name should also be defined in the namespace declaration:
<ContentPage ... xmlns:controls="clr-namespace:Controls;assembly=MyControlLibrary" ...>
...
</ContentPage>
The namespace prefix is then specified when declaring an instance of a type from an imported namespace:
<controls:Expander IsExpanded="True">
...
</controls:Expander>
For information about defining a custom namespace schema, see Custom namespace schemas.