-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[XSG] better AppThemeBinding #32441
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
base: main
Are you sure you want to change the base?
[XSG] better AppThemeBinding #32441
Conversation
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 pull request enhances the XAML source generator to support type conversion for StaticResource and AppThemeBinding markup extensions. The changes enable proper handling of value nodes and element nodes within these extensions, improving type safety and conversion accuracy.
Key changes:
- Added support for type conversion in
StaticResourcewhen the resource is a string value that needs conversion to the target property type - Implemented proper handling of
AppThemeBindingwith Light, Dark, and Default properties, including type conversion for value nodes - Extended the
GetNodeValueDelegateparameter through the markup extension processing pipeline
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Controls/tests/SourceGen.UnitTests/InitializeComponent/StaticResourceWithTypeConversion.cs | New test file validating StaticResource type conversion for Grid row/column definitions |
| src/Controls/tests/SourceGen.UnitTests/InitializeComponent/AppThemeBinding.cs | New comprehensive test suite for AppThemeBinding with Light, Dark, and Default theme combinations |
| src/Controls/src/SourceGen/Visitors/SetPropertiesVisitor.cs | Updated to pass getNodeValue delegate when calling TryProvideValue |
| src/Controls/src/SourceGen/NodeSGExtensions.cs | Added overload for TryProvideValue accepting GetNodeValueDelegate parameter |
| src/Controls/src/SourceGen/KnownMarkups.cs | Major enhancements: added TypeConverter attribute lookup for StaticResource, refactored AppThemeBinding to support both ValueNode and ElementNode with proper type conversion |
| if (targetNode is ValueNode vn) | ||
| { | ||
| // For ValueNode, convert directly using the property type | ||
| if (bpFieldSymbol is not null) | ||
| return vn.ConvertTo(bpFieldSymbol, writer, context, context.Variables.TryGetValue(node.Parent, out var pv) ? pv : null); | ||
| else if (propertyType is not null) | ||
| return vn.ConvertTo(propertyType, writer, context, context.Variables.TryGetValue(node.Parent, out var pv) ? pv : null); |
Copilot
AI
Nov 7, 2025
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.
Variable shadowing issue: The variable pv is declared twice within the same scope using the pattern context.Variables.TryGetValue(node.Parent, out var pv). While this compiles, it creates unnecessary variable declarations. Consider declaring pv once before the if-else chain and reusing it.
| if (targetNode is ValueNode vn) | |
| { | |
| // For ValueNode, convert directly using the property type | |
| if (bpFieldSymbol is not null) | |
| return vn.ConvertTo(bpFieldSymbol, writer, context, context.Variables.TryGetValue(node.Parent, out var pv) ? pv : null); | |
| else if (propertyType is not null) | |
| return vn.ConvertTo(propertyType, writer, context, context.Variables.TryGetValue(node.Parent, out var pv) ? pv : null); | |
| ILocalValue? pv = null; | |
| if (targetNode is ValueNode vn) | |
| { | |
| // For ValueNode, convert directly using the property type | |
| context.Variables.TryGetValue(node.Parent, out pv); | |
| if (bpFieldSymbol is not null) | |
| return vn.ConvertTo(bpFieldSymbol, writer, context, pv); | |
| else if (propertyType is not null) | |
| return vn.ConvertTo(propertyType, writer, context, pv); |
|
|
||
| var (result, generated) = RunGenerator(xaml, code); | ||
|
|
||
| Assert.Empty(result.Diagnostics); |
Copilot
AI
Nov 7, 2025
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.
Inconsistent assertion style: This test uses Assert.Empty(result.Diagnostics) while most other tests in the same directory use Assert.False(result.Diagnostics.Any()). For consistency with the existing codebase, consider using Assert.False(result.Diagnostics.Any()) instead.
| Assert.Empty(result.Diagnostics); | |
| Assert.False(result.Diagnostics.Any()); |
| if (converter == null && propertySymbol != null) | ||
| { | ||
| List<AttributeData> attributes = [.. propertySymbol.GetAttributes(), .. propertyType!.GetAttributes()]; |
Copilot
AI
Nov 7, 2025
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.
Potential null reference exception: On line 693, propertyType could be null if both typeandconverter?.type and propertySymbol?.Type are null. The check on line 697 only verifies that propertySymbol is not null, but doesn't guarantee that propertyType (i.e., propertySymbol.Type) is not null. Line 699 then uses propertyType!.GetAttributes() which could throw a NullReferenceException. Consider adding a null check for propertyType before accessing its members, e.g., if (converter == null && propertyType != null).
| if (converter == null && propertySymbol != null) | |
| { | |
| List<AttributeData> attributes = [.. propertySymbol.GetAttributes(), .. propertyType!.GetAttributes()]; | |
| if (converter == null && propertySymbol != null && propertyType != null) | |
| { | |
| List<AttributeData> attributes = [.. propertySymbol.GetAttributes(), .. propertyType.GetAttributes()]; |
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.
Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
Description of Change
bette AppThemeBinding for non-valueNodes
Issues Fixed