-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix for RelativeSource AncestorType bindings not generating compiled bindings under AOT #34408
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
BagavathiPerumal
wants to merge
2
commits into
dotnet:main
Choose a base branch
from
BagavathiPerumal:fix-34056
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
| xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests" | ||
| x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui34056" | ||
| x:DataType="local:Maui34056PageViewModel"> | ||
| <VerticalStackLayout> | ||
| <!-- Scenario 1: RelativeSource AncestorType inside DataTemplate with x:DataType (issue fix) --> | ||
| <CollectionView x:Name="TestCollectionView" | ||
| ItemsSource="{Binding Items}"> | ||
| <CollectionView.ItemTemplate> | ||
| <DataTemplate x:DataType="local:Maui34056ItemViewModel"> | ||
| <Button x:Name="TestButton" | ||
| Text="{Binding ItemName}" | ||
| Command="{Binding x:DataType='local:Maui34056PageViewModel', Source={RelativeSource AncestorType={x:Type local:Maui34056PageViewModel}}, Path=TestCommand}" /> | ||
| </DataTemplate> | ||
| </CollectionView.ItemTemplate> | ||
| </CollectionView> | ||
|
|
||
| <!-- Scenario 2: {RelativeSource Self} inside DataTemplate with x:DataType. | ||
| SourceGen must not use x:DataType as the source type; the source is the element itself. | ||
| Path=ItemName exists on Maui34056ItemViewModel to ensure the guard, not a failed lookup, prevents compiled binding. --> | ||
| <CollectionView x:Name="SelfBindingCollectionView" | ||
| ItemsSource="{Binding Items}"> | ||
| <CollectionView.ItemTemplate> | ||
| <DataTemplate x:DataType="local:Maui34056ItemViewModel"> | ||
| <Label x:Name="SelfBindingLabel" | ||
| Text="{Binding Path=ItemName, Source={RelativeSource Self}}" /> | ||
| </DataTemplate> | ||
| </CollectionView.ItemTemplate> | ||
| </CollectionView> | ||
| </VerticalStackLayout> | ||
| </ContentPage> |
93 changes: 93 additions & 0 deletions
93
src/Controls/tests/Xaml.UnitTests/Issues/Maui34056.xaml.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| using System.Collections.ObjectModel; | ||
| using System.Windows.Input; | ||
| using Microsoft.Maui.Controls.Internals; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Maui.Controls.Xaml.UnitTests; | ||
|
|
||
| // Page-level ViewModel — this is what the RelativeSource AncestorType points to | ||
| public class Maui34056PageViewModel | ||
| { | ||
| public ObservableCollection<Maui34056ItemViewModel> Items { get; } = | ||
| [new Maui34056ItemViewModel { ItemName = "Item 1" }]; | ||
|
|
||
| public ICommand TestCommand { get; } = new Command(() => { }); | ||
| } | ||
|
|
||
| // Item ViewModel — this is what the DataTemplate's x:DataType is set to | ||
| public class Maui34056ItemViewModel | ||
| { | ||
| public string ItemName { get; set; } = ""; | ||
| } | ||
|
|
||
| public partial class Maui34056 : ContentPage | ||
| { | ||
| public Maui34056() | ||
| { | ||
| InitializeComponent(); | ||
| BindingContext = new Maui34056PageViewModel(); | ||
| } | ||
|
|
||
| [Collection("Issue")] | ||
| public class Maui34056Tests | ||
BagavathiPerumal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| [Theory] | ||
| [XamlInflatorData] | ||
| internal void RelativeSourceAncestorTypeInDataTemplateGeneratesCompiledBinding(XamlInflator inflator) | ||
| { | ||
| var page = new Maui34056(inflator); | ||
|
|
||
| var template = ((CollectionView)page.TestCollectionView).ItemTemplate; | ||
| var content = template.CreateContent() as Button; | ||
| Assert.NotNull(content); | ||
|
|
||
| var bindingContext = content.GetContext(Button.CommandProperty); | ||
| Assert.NotNull(bindingContext); | ||
| var binding = bindingContext.Bindings.GetValue(); | ||
|
|
||
| if (inflator is XamlInflator.Runtime) | ||
| { | ||
| // Runtime inflator uses the string-based Binding — no compile-time type info available. | ||
| Assert.IsType<Binding>(binding); | ||
| } | ||
| else | ||
| { | ||
| // Both XamlC and SourceGen produce a trim-safe TypedBinding when x:DataType is present | ||
| // on the binding node alongside RelativeSource AncestorType. | ||
| Assert.IsType<TypedBinding<Maui34056PageViewModel, ICommand>>(binding); | ||
| } | ||
| } | ||
|
|
||
| [Theory] | ||
| [XamlInflatorData] | ||
| internal void RelativeSourceSelfInDataTemplateWithXDataTypeUsesStringBinding(XamlInflator inflator) | ||
| { | ||
| // Verifies SourceGen does not use the DataTemplate's x:DataType as the source type for | ||
| // {RelativeSource Self} bindings. The source is the element itself, resolved at runtime. | ||
| // Path=ItemName exists on Maui34056ItemViewModel to ensure the guard is what prevents | ||
| // compiled binding, not a failed type lookup. XamlC behavior is pre-existing and separate. | ||
| var page = new Maui34056(inflator); | ||
|
|
||
| var template = ((CollectionView)page.SelfBindingCollectionView).ItemTemplate; | ||
| var content = template.CreateContent() as Label; | ||
| Assert.NotNull(content); | ||
|
|
||
| var bindingContext = content.GetContext(Label.TextProperty); | ||
| Assert.NotNull(bindingContext); | ||
| var binding = bindingContext.Bindings.GetValue(); | ||
|
|
||
| if (inflator is XamlInflator.XamlC) | ||
| { | ||
| // XamlC pre-existing behavior: compiles RelativeSource Self using DataTemplate x:DataType. | ||
| // This is a separate issue, not addressed by this fix. | ||
| Assert.IsType<TypedBinding<Maui34056ItemViewModel, string>>(binding); | ||
| } | ||
| else | ||
| { | ||
| // Runtime: no compile-time type info, always string-based Binding. | ||
| // SourceGen (the fix): HasRelativeSourceBinding blocks x:DataType path for Self bindings. | ||
| Assert.IsType<Binding>(binding); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
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.
Source={RelativeSource AncestorType={local:Maui34056PageViewModel}}is also valid syntax and in that case I believeancestorTypeNodewould be aValueNodewith a string value, although I might be wrong. We should have a test case for this scenario too and make sure it works too.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.
@simonrozsival, I investigated the syntax {local:Maui34056PageViewModel} and confirmed that it is not valid XAML.
The XAML compiler throws MAUIG1001: "Invalid RelativeSource Mode '(none)'" at build time because {local:...} is not a markup extension and cannot return a System.Type.
Based on official documentation, the AncestorType property must always receive a valid Type. As documented:
“The AncestorType property must be set to a Type… otherwise a XamlParseException is thrown.”
Therefore, the only valid syntax is:
{x:Type local:Maui34056PageViewModel}
This valid form is already handled by the current fix, so no additional code change or test is required for this scenario.
Documentation: https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/data-binding/relative-bindings?view=net-maui-10.0#bind-to-an-ancestor