Skip to content

XAML xmlns simplifications #29579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

StephaneDelcroix
Copy link
Contributor

@StephaneDelcroix StephaneDelcroix commented May 19, 2025

Description of Change

This PR includes multiple changes related to xmlns, as tracked by #28836.

New global xmlns

http://schemas.microsoft.com/dotnet/maui/global is a new xmlns that you can use to aggregate multiple xmlns together.

The scope of it is local to the project.

By default, it contains the maui xmlns (sourcegenerated) YourNamespace and YourNamespace.Pages (enabled by the updated template.

You can extend this in e.g. GlobalXmlns.cs added by the template (#29203), or adding assembly level XmlnsDefinition anywhere.

You can ad CLR namespaces as usual, but also other xmlns

[assembly:XmlnsDefinition("http://schemas.microsoft.com/dotnet/maui/global", "http://mycompany.com/schema/myxmlns")]

That global xmlns also have a XmlnsPrefix "global" associated to it.

IMPORTANT NOTE: you can not aggregate the x xmlns into the global one. It plays a special role for the parsers and inflators.

This change is implemented using a source generator, so there's no requirement to change any other tool consuming xaml making use of this

Make the xmlns delcarations in XAML implicit

The XAML inflators uses the global xmlns as default, and preloads all other xmlns declared by a XmlnsDefinition that also have a XmlnsPrefix attribute. e.g. the x: prefix is there to be used without the need of being declared.

<ContentPage x:Class="MyNamespace.MyPage" Title="no default xmlns">
    <VerticalStackLayout>
        <Label x:Name="label" Text="No xmlns:x declaration, but x: usage anyway" />
    </VerticalStackLayout>
</ContentPage>

This part is opt-in, you can enable it by setting

  <PropertyGroup>
    <DefineConstants>$(DefineConstants);MauiAllowImplicitXmlnsDeclaration</DefineConstants>
    <EnablePreviewFeatures>true</EnablePreviewFeatures>
  </PropertyGroup>

in your .csproj

Protect some xmlns

with a fix for #28150, it becomes necessary to avoid the overload of the maui, and x, xmlns namespaces.

Issues Fixed

Make the default xmlns implicitely pointing to maui default xmlns
pre-load all available XmlsPrefixes for use

the capability is disabled by default, and you need to opt-in

- fixes #28849
- fixes #28850
- fixes #28847
Allow aggregating multiple xmlns into a single new global
http://schemas.microsoft.com/dotnet/maui/global using XmlnsDefinition
attribute, like this

```csharp
[assembly: XmlnsDefinition("http://schemas.microsoft.com/dotnet/maui/global", "http://schemas.microsoft.com/dotnet/2021/maui")]
```

the x: xmlns can not be aggregated, as it serves important purposes for
the parsers

this also brings #28090 to fix #28150

- fixes #28150
- fixes #28843
- closes #28090
@Copilot Copilot AI review requested due to automatic review settings May 19, 2025 19:24
@StephaneDelcroix StephaneDelcroix requested a review from a team as a code owner May 19, 2025 19:24
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds a new global XAML xmlns, enables implicit xmlns declarations via a preview feature flag, protects built-in xmlns entries, and updates related source generators and metadata.

  • Introduces AllowImplicitXmlnsDeclarationAttribute and wiring in build tasks to support implicit/default xmlns usage.
  • Changes XmlnsDefinitionAttribute constructor parameter name and surfaces a new Target property.
  • Updates public API shipped/unshipped files, documentation, and solution to expose new types and behaviors.

Reviewed Changes

Copilot reviewed 42 out of 42 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
PublicAPI/netstandard/PublicAPI.Shipped.txt Updated XmlnsDefinitionAttribute ctor signature
PublicAPI/net/*.Unshipped.txt & *.Shipped.txt (all platforms) Added AllowImplicitXmlnsDeclarationAttribute and Target API
Properties/AssemblyInfo.cs Re-added x: xmlns prefix for XAML
AllowImplicitXmlnsDeclarationAttribute.cs New attribute to opt into implicit xmlns support
Build.Tasks/XmlTypeExtensions.cs Extended XML namespace gathering and added prefix extraction
Build.Tasks/XamlTask.cs Wired implicit xmlns logic into XAML parsing
Build.Tasks/XamlCTask.cs Updated XAML compilation to pass module context
docs/XmlnsDefinitionAttribute.xml Updated ctor sig in docs and added Target member
Microsoft.Maui-vscode.sln Included new SourceGen unit test project
Comments suppressed due to low confidence (1)

src/Controls/src/Build.Tasks/XmlTypeExtensions.cs:45

  • The new logic protecting built-in xmlns definitions and throwing BuildException on invalid entries lacks corresponding unit tests. Please add tests covering both allowed and prohibited XmlnsDefinitionAttribute scenarios to validate this behavior.
if (   attribute.XmlNamespace != XamlParser.MauiGlobal

@StephaneDelcroix StephaneDelcroix changed the base branch from main to net10.0 May 19, 2025 19:39
@StephaneDelcroix StephaneDelcroix changed the base branch from net10.0 to main May 19, 2025 19:42
protect maui and x: xmlns from overloading.

- fixes #28836
@StephaneDelcroix StephaneDelcroix added this to the .NET 10 Planning milestone May 20, 2025
@jfversluis jfversluis added the area-xaml XAML, CSS, Triggers, Behaviors label May 20, 2025
@StephaneDelcroix StephaneDelcroix force-pushed the dev/stdelc/xmlns2025 branch 2 times, most recently from 7775f78 to 302c11b Compare May 22, 2025 10:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-xaml XAML, CSS, Triggers, Behaviors
Projects
None yet
2 participants