Description
Recently, the .NET MAUI team has added support for this exact feature in .NET 10, as described in their release notes. It introduces Global XML Namespaces and Implicit XML Namespaces for XAML, which dramatically improves developer experience and reduces boilerplate in XAML files.
In your projects you can now glob together XML-namespaces into a new global namespace xmlns="http://schemas.microsoft.com/dotnet/maui/global", and use them without prefixes. The .NET MAUI namespace is included already.
By convention the new project template creates a file GlobalXmlns.cs.
[assembly: XmlnsDefinition(
"http://schemas.microsoft.com/dotnet/maui/global",
"MyApp.Views")]
[assembly: XmlnsDefinition(
"http://schemas.microsoft.com/dotnet/maui/global",
"MyApp.Controls")]
[assembly: XmlnsDefinition(
"http://schemas.microsoft.com/dotnet/maui/global",
"MyApp.Converters")]
[assembly: XmlnsDefinition(
"http://schemas.microsoft.com/dotnet/maui/global",
"http://schemas.syncfusion.com/maui/toolkit")]
You can then use anything in those namespaces like you do .NET MAUI controls, without prefix.
<ContentPage
xmlns="http://schemas.microsoft.com/dotnet/maui/global"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.MainPage">
<TagView x:DataType="Tag" />
</ContentPage>
The x namespace cannot be globbed into the global namespace since it's used for parsing.
You can opt-in to this feature by adding the following to your project file. As of now, turning this on may cause errors to be reported by various XAML tools.
<PropertyGroup>
<DefineConstants>$(DefineConstants);MauiAllowImplicitXmlnsDeclaration</DefineConstants>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
</PropertyGroup>
With this change you can also eliminate xmlns and xmlns:x from XAML files.
<ContentPage x:Class="MyApp.MainPage">
<TagView x:DataType="Tag" />
</ContentPage>
In this usage:
- the default xmlns is the global one
- the x: prefix is added by default
- all xmlns with a XmlnsPrefix are also accessible with their prefix, even if they are included in the global xmlns. These are useful for disambiguating a name. For example, the maui: prefix points to the maui xmlns.
- you still need to use the x: prefix (e.g. x:Class, x:DataType), but you don't have to declare it
This feature has been requested several times in the past by the community, But they turned into discussions and reached no conclusion.: