Skip to content

Add Support for Global and Implicit XML Namespaces in WinUI XAML (parity with .NET MAUI) #10616

Open
@ghost1372

Description

@ghost1372

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.

XAML Global Namespaces

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.

XAML Implicit Namespaces

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.:

#10293

microsoft/WindowsAppSDK#2946

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triageIssue needs to be triaged by the area owners

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions