Repository navigation
[XSG] Fix RelativeSource binding with FindAncestorBindingContext mode - #32925
Conversation
…text mode Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
…ed test Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
simonrozsival
left a comment
There was a problem hiding this comment.
@copilot looks great! I'd like to also add a SourceGen.UnitTests which will make sure that the generated code for the XAML just as Maui32924.xaml does not contain XamlTypeResolver (which would produce trimming and AOT warnings).
…TypeResolver usage Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Added |
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
There was a problem hiding this comment.
Pull request overview
This PR fixes RelativeSource bindings with FindAncestorBindingContext mode when the XAML source generator (XSG) is enabled. The issue was that RelativeSourceExtension was not registered in the source generator's known markup extensions, causing bindings to fail at runtime.
Key changes:
- Adds source generator support for
RelativeSourceExtensionwith proper code generation for all modes - Implements automatic mode inference (Element types → FindAncestor, non-Element types → FindAncestorBindingContext)
- Adds comprehensive test coverage for all RelativeSource modes across all inflators
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
src/Controls/src/SourceGen/KnownMarkups.cs |
Implements ProvideValueForRelativeSourceExtension to generate correct RelativeBindingSource instantiation code for all modes with proper mode inference |
src/Controls/src/SourceGen/NodeSGExtensions.cs |
Registers RelativeSourceExtension in the known early markup extensions dictionary |
src/Controls/tests/Xaml.UnitTests/Issues/Maui32924.xaml |
Adds XAML test cases for explicit/implicit FindAncestorBindingContext, FindAncestor, and Self modes |
src/Controls/tests/Xaml.UnitTests/Issues/Maui32924.xaml.cs |
Adds unit tests validating all RelativeSource modes work across all inflators (Runtime, XamlC, SourceGen) |
src/Controls/tests/SourceGen.UnitTests/InitializeComponent/RelativeSourceBindings.cs |
Adds source generator unit tests verifying generated code doesn't use XamlTypeResolver (preventing trimming/AOT warnings) |
|
I went through the failing UI tests and I'm confident they are all unrelated. |
<!-- Please let the below note in for people that find this PR --> > [!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](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from this PR and let us know in a comment if this change resolves your issue. Thank you! ### Description When a binding has a `Source` property with a `RelativeSourceExtension`, skip the compiled binding path and use the fallback string-based binding instead. ### Root Cause When PR #32925 added RelativeSource support to SourceGen, it also inadvertently caused bindings with `RelativeSource` to be compiled using `x:DataType` as the source type. This is incorrect because the source type for `RelativeSource` bindings is determined at runtime, not at compile time. For example, with: ```xml <ContentView x:DataType="local:ChildViewModel"> <TapGestureRecognizer CommandParameter="{Binding Path=., Source={RelativeSource AncestorType={x:Type local:MainPage}}}" /> </ContentView> ``` The SourceGen was generating: ```csharp return new TypedBinding<ChildViewModel, ChildViewModel>( getter: source => (source, true), // Returns ChildViewModel, not MainPage! ... ); ``` ### Fix Added a check in `ProvideValueForBindingExtension` to detect when the binding has a `Source` property with a `RelativeSourceExtension`. In this case, we skip the compiled binding path and use the fallback string-based binding instead, which correctly resolves the source at runtime. ### Issues Fixed Fixes #33247 ### Testing - Added SourceGen unit tests to verify correct code generation - Added XAML unit tests to verify the binding works correctly at runtime
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
RelativeSourceExtensionwas not registered in the source generator's known markup extensions, causing bindings with{RelativeSource AncestorType=..., Mode=FindAncestorBindingContext}to fail at runtime when XSG is enabled.Changes:
ProvideValueForRelativeSourceExtensioninKnownMarkups.csto generate correct code for all RelativeSource modes (Self,TemplatedParent,FindAncestor,FindAncestorBindingContext)RelativeSourceExtensioninGetKnownEarlyMarkupExtensionsModenot specified: Element types →FindAncestor, non-Element types →FindAncestorBindingContextMaui32924Xaml.UnitTests to validate the fix across all inflators (Runtime, XamlC, SourceGen)RelativeSourceBindingsSourceGen.UnitTests to verify generated code does not containXamlTypeResolver(which would produce trimming and AOT warnings)Example that now works with XSG:
Issues Fixed
Fixes #32924
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.