|
1 | 1 | --- |
2 | 2 | title: "XAML namespaces" |
3 | 3 | description: ".NET MAUI XAML uses the xmlns XML attribute for namespace declarations. The default namespace specifies that elements defined within the XAML file with no prefix refer to .NET MAUI classes." |
4 | | -ms.date: 10/19/2023 |
| 4 | +ms.date: 07/15/2025 |
5 | 5 | --- |
6 | 6 |
|
7 | 7 | # XAML namespaces |
8 | 8 |
|
9 | | -XAML uses the `xmlns` XML attribute for namespace declarations. There are two XAML namespace declarations that are always within the root element of a XAML file. The first defines the default namespace: |
| 9 | +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: |
10 | 10 |
|
11 | 11 | ```xaml |
12 | 12 | xmlns="http://schemas.microsoft.com/dotnet/2021/maui" |
@@ -43,6 +43,67 @@ For more information about the `x:ClassModifier` attribute, see [Class modifiers |
43 | 43 |
|
44 | 44 | 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. |
45 | 45 |
|
| 46 | +## Implicit namespace declarations |
| 47 | + |
| 48 | +::: moniker range=">=net-maui-11.0" |
| 49 | + |
| 50 | +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: |
| 51 | + |
| 52 | +- `xmlns="http://schemas.microsoft.com/dotnet/2021/maui"` — the default .NET MAUI namespace. |
| 53 | +- `xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"` — the XAML language namespace providing `x:Class`, `x:Name`, and other intrinsic constructs. |
| 54 | + |
| 55 | +This means a content page that previously required explicit namespace declarations: |
| 56 | + |
| 57 | +```xaml |
| 58 | +<!-- .NET 10 and earlier: explicit namespace declarations required --> |
| 59 | +<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" |
| 60 | + xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" |
| 61 | + x:Class="MyApp.MainPage"> |
| 62 | + <Label Text="Hello, World!" /> |
| 63 | +</ContentPage> |
| 64 | +``` |
| 65 | + |
| 66 | +Can now be written without them: |
| 67 | + |
| 68 | +```xaml |
| 69 | +<!-- .NET 11: standard namespaces are implicit --> |
| 70 | +<ContentPage x:Class="MyApp.MainPage"> |
| 71 | + <Label Text="Hello, World!" /> |
| 72 | +</ContentPage> |
| 73 | +``` |
| 74 | + |
| 75 | +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. |
| 76 | + |
| 77 | +> [!IMPORTANT] |
| 78 | +> Custom namespaces still require explicit `xmlns:` declarations. Only the default .NET MAUI namespace and the `x:` XAML language namespace are implicitly available. |
| 79 | +
|
| 80 | +For example, you still need to declare a prefix for your own CLR namespaces or third-party libraries: |
| 81 | + |
| 82 | +```xaml |
| 83 | +<ContentPage x:Class="MyApp.MainPage" |
| 84 | + xmlns:local="clr-namespace:MyApp.Controls" |
| 85 | + xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"> |
| 86 | + <local:CustomControl /> |
| 87 | +</ContentPage> |
| 88 | +``` |
| 89 | + |
| 90 | +::: moniker-end |
| 91 | + |
| 92 | +::: moniker range="net-maui-10.0" |
| 93 | + |
| 94 | +In .NET 10, implicit namespace declarations are available as a preview feature. To opt in, add the following to your project file: |
| 95 | + |
| 96 | +```xml |
| 97 | +<PropertyGroup> |
| 98 | + <DefineConstants>$(DefineConstants);MauiAllowImplicitXmlnsDeclaration</DefineConstants> |
| 99 | + <EnablePreviewFeatures>true</EnablePreviewFeatures> |
| 100 | +</PropertyGroup> |
| 101 | +``` |
| 102 | + |
| 103 | +When enabled, you can omit the standard `xmlns` and `xmlns:x` declarations from your XAML files. Custom namespaces still require explicit `xmlns:` declarations. |
| 104 | + |
| 105 | +::: moniker-end |
| 106 | + |
46 | 107 | ## Declare namespaces for types |
47 | 108 |
|
48 | 109 | 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: |
|
0 commit comments