Optimize DataTemplateExtension as KnownMarkup in source generator#32761
Merged
StephaneDelcroix merged 1 commit intomainfrom Nov 20, 2025
Merged
Optimize DataTemplateExtension as KnownMarkup in source generator#32761StephaneDelcroix merged 1 commit intomainfrom
StephaneDelcroix merged 1 commit intomainfrom
Conversation
…ation - Add ProvideValueForDataTemplateExtension to KnownMarkups.cs - Register DataTemplateExtension in GetKnownEarlyMarkupExtensions - Create comprehensive unit tests for DataTemplate markup extension - Generate direct DataTemplate constructor calls instead of ProvideValue pattern - Reduces generated code from ~40 lines to ~2 lines per usage - All 75 SourceGen unit tests pass
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR successfully optimizes DataTemplateExtension by implementing it as a KnownMarkup in the XAML source generator, achieving ~95% code reduction and eliminating reflection overhead. The implementation follows the established pattern used by TypeExtension and other early markup extensions.
Key Changes
- Added
ProvideValueForDataTemplateExtensionmethod that resolves type names at compile-time and generates directDataTemplateconstructor calls - Registered
DataTemplateExtensionas an early markup extension alongsideTypeExtension,StaticExtension, etc. - Added comprehensive unit tests validating the optimized code generation for both custom and built-in types
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/Controls/tests/SourceGen.UnitTests/InitializeComponent/DataTemplateMarkupExtension.cs |
New test file with two tests covering DataTemplate usage with custom ViewCell types and built-in Label controls, validating simplified generated code |
src/Controls/src/SourceGen/NodeSGExtensions.cs |
Registration of DataTemplateExtension in GetKnownEarlyMarkupExtensions dictionary to enable compile-time optimization |
src/Controls/src/SourceGen/KnownMarkups.cs |
Core implementation that resolves TypeName to Type at compile-time and generates direct constructor calls, following TypeExtension pattern with proper error handling and fallback logic |
Contributor
Author
|
/azp run MAUI-UITests-public |
|
Azure Pipelines successfully started running 1 pipeline(s). |
simonrozsival
approved these changes
Nov 20, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
This PR implements
DataTemplateExtensionas a KnownMarkup in the XAML source generator, reducing generated code size by ~95% and improving performance through direct constructor calls instead of reflection-basedProvideValue.Changes Made
Source Generator Implementation
KnownMarkups.cs:
ProvideValueForDataTemplateExtensionmethodnew DataTemplate(typeof(...))constructor callsNodeSGExtensions.cs:
DataTemplateExtensioninGetKnownEarlyMarkupExtensionsUnit Tests
DataTemplateMarkupExtension.cs:
DataTemplateMarkupExtensionWithType: Tests with custom ViewCell classDataTemplateMarkupExtensionInListView: Tests with built-in Label controlCode Generation Comparison
Before (ProvideValue pattern - ~40 lines)
After (KnownMarkup - ~2 lines)
Benefits
Testing
✅ All 75 SourceGen unit tests pass
✅ 2 new DataTemplate-specific tests added and passing
✅ No regressions in existing functionality
Issues Fixed
Fixes #32760