-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Source Generator for XAML #30442
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
Source Generator for XAML #30442
Conversation
127d9f2 to
97cf9ed
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR integrates a new XAML source generator, standardizing XAML inflation across Debug and Release builds, replacing legacy overloads, and updating tests to target multiple inflators (Runtime, XamlC, SourceGen).
- Added
[XamlProcessing]attributes to pages to control which inflate modes apply, and removed oldbool useCompiledXamloverloads in favor of a single-parameter constructor. - Refactored tests to use
[Values] XamlInflatorinstead of boolean[TestCase], with branches covering Runtime, XamlC, and SourceGen. - Updated test helpers (
XamlInflatorRuntimeTestsHelpers) and test assertions to verify generated code and diagnostics for each inflate mode.
Reviewed Changes
Copilot reviewed 272 out of 514 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Controls/tests/Xaml.UnitTests/Issues/Gh3512.xaml.cs | Test updated to cover SourceGen diagnostics branch |
| src/Controls/tests/Xaml.UnitTests/InflatorSwitch/XamlInflatorSourceGen.xaml.cs | Added source-gen-specific inflator test |
| src/Controls/tests/Xaml.UnitTests/InflatorSwitch/XamlInflatorRuntimeTestsHelpers.cs | Introduced test helper for verifying inflate-specific IL sizes |
Files not reviewed (2)
- src/Controls/src/SourceGen/MauiGResources.Designer.cs: Language not supported
- src/Controls/src/SourceGen/xlf/MauiGResources.Designer.cs: Language not supported
Comments suppressed due to low confidence (2)
src/Controls/tests/Xaml.UnitTests/Issues/Bz60575.xaml.cs:25
- After generating with
MockSourceGenerator, add an assertion onresult.Diagnostics(e.g.,Assert.That(result.Diagnostics, Is.Empty)) to ensure SourceGen produces no unexpected diagnostics.
{
src/Controls/tests/Xaml.UnitTests/InflatorSwitch/XamlInflatorRuntimeTestsHelpers.cs:17
- The
GetConstructor([typeof(XamlInflator)])syntax is invalid. Use an array initializer:GetConstructor(new[] { typeof(XamlInflator) }).
Assert.IsNotNull(type.GetConstructor([typeof(XamlInflator)]), $"{type.Name} should have InitializeComponent method with XamlInflator argument");
dd0db68 to
46c2ab2
Compare
|
see #30489 |
c5991e6 to
ad330c5
Compare
ff3fe7f to
074aa42
Compare
3b477b4 to
c42b1f1
Compare
| || x.ExternalReferences.Length != y.ExternalReferences.Length) | ||
| return false; | ||
|
|
||
| return x.ExternalReferences.OfType<PortableExecutableReference>().SequenceEqual(y.ExternalReferences.OfType<PortableExecutableReference>()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are the external references always in the same order?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding a comment to look at that
c42b1f1 to
795d06f
Compare
795d06f to
008e334
Compare
a5e1d18 to
513891d
Compare
This is Xaml Source generation. After Runtime, and XamlC, SourceGen is a third Xaml Inflator for MAUI. Runtime inflation is used in Debug scenario cause it doesn't slow down the devloop, and is easily instrumented. Most of the toolin for VS depend on diagnostic generated only by the runtime inflation. But it is also quite slow at runtime, use reflection to find and create types and set properties and prevents AOT scenarios XAMLC inflation is used for Release, and replace all the XAML by the equivalent IL. it takes a handfull of seconds as is rewrites assemblies but it's as fast as possible while running. The downside is that it's hard to maintain, hard to evolve, hard to debug, and it's hard to reflect about the code generated. SourceGen inflation, the new one, will eventually replace both. As it generates C# before the compilation, the devloop impact is barely noticeable. It's easy to instrument, it produces code as fast as XamlC does, it's easy to debug (it's C#), the output is easy ot read, easy to reason about. And as a bonus it'll allow MAUI to innovate with XAML again.
513891d to
5f52706
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is awesome! I tested our developer balance template and the performance is night and day in debug mode!
Without Source Gen
Untitled.mov
With Source Gen
Screen.Recording.2025-07-25.at.5.06.01.PM.mov
The testing here looks to maintain pre-source gen validation so we're good to merge for preview7 and then get as much feedback as we can.
The one issue I ran into with the developer balance template was the following error in the source gen
maui/src/Templates/src/templates/maui-mobile/Pages/Controls/ChipDataTemplateSelector.cs
Line 5 in 7195ecc
| public class ChipDataTemplateSelector : DataTemplateSelector |
This gave me the following error

If I change these to nullable it works
public class ChipDataTemplateSelector : DataTemplateSelector
{
public DataTemplate? SelectedTagTemplate { get; set; }
public DataTemplate? NormalTagTemplate { get; set; }
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
var isSelected = (item as Tag)?.IsSelected ?? false;
return isSelected ? (SelectedTagTemplate ?? new DataTemplate()) : (NormalTagTemplate ?? new DataTemplate());
}
}These are specified on the XAML so I'm assuming they should be generating ?
@StephaneDelcroix Could you please just explain what's the cause of the speedup? Will we see similar speedup on Windows as well? |
|
@MartyIX you'll see shortened devloop, speedup on Debug, and Release should be faster as well but not by the same factor. with this (disabled by default for the time being), Xaml. inflation on Debug builds no longer uses reflection, causing the speedup |
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Description of Change
This is Xaml Source generation. After Runtime, and XamlC, SourceGen is a third Xaml Inflator for MAUI.
Runtime inflation is used in Debug scenario cause it doesn't slow down the devloop, and is easily instrumented. Most of the tooling for VS depend on diagnostic generated only by the runtime inflation. But it is also quite slow at runtime, use reflection to find and create types and set properties and prevents AOT scenarios
XAMLC inflation is used for Release, and replace all the XAML by the equivalent IL. it takes a handfull of seconds as is rewrites assemblies but it's as fast as possible while running. The downside is that it's hard to maintain, hard to evolve, hard to debug, and it's hard to reflect about the code generated.
SourceGen inflation, the new one, will eventually replace both.
As it generates C# before the compilation, the devloop impact is barely noticeable. It's easy to instrument, it produces code as fast as XamlC does (and is already faster in some cases), it's easy to debug (it's C#), the output is easy to read, easy to reason about.
And as a bonus it'll allow MAUI to innovate with XAML again.
The plan is to merge this soonish in net10.0 and keep the feature behind a feature flag. It is important to review the areas that were modified but that do not pertain to sourcegen, to ensure we do not regress anything in net10.0
This is unfinished work, in terms of feature (still a few unsupported scenarios), and code quality (FIXME here and there, error reporting, extensibility points, etc) but the branch is too large to be maintained on it's own, it's too expensive to merge or rebase.
how to use
grab the nugets from the artefacts, use them in a projects, enable preview features, and decorate your code behind type with [XamlProcessing(XamlInflator.SourceGen)]
Fixes