-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
[XC] Fix x:DataType resolution for BindingContext #21454
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using Microsoft.Maui.ApplicationModel; | ||
using Microsoft.Maui.Controls.Core.UnitTests; | ||
using Microsoft.Maui.Dispatching; | ||
|
||
using Microsoft.Maui.UnitTests; | ||
using NUnit.Framework; | ||
|
||
namespace Microsoft.Maui.Controls.Xaml.UnitTests; | ||
|
||
public partial class Maui21434 | ||
{ | ||
public Maui21434() | ||
{ | ||
InitializeComponent(); | ||
} | ||
|
||
public Maui21434(bool useCompiledXaml) | ||
{ | ||
//this stub will be replaced at compile time | ||
} | ||
|
||
[TestFixture] | ||
class Test | ||
{ | ||
[SetUp] | ||
public void Setup() | ||
{ | ||
Application.SetCurrentApplication(new MockApplication()); | ||
DispatcherProvider.SetCurrent(new DispatcherProviderStub()); | ||
} | ||
|
||
[TearDown] public void TearDown() => AppInfo.SetCurrent(null); | ||
|
||
[Test] | ||
public void BindingsDoNotResolveStaticProperties([Values(false, true)] bool useCompiledXaml) | ||
{ | ||
var page = new Maui21434(useCompiledXaml); | ||
Assert.That(page.ParentTextLabel?.Text, Is.EqualTo("ParentText")); | ||
Assert.That(page.ChildTextLabel?.Text, Is.EqualTo("ChildText")); | ||
} | ||
} | ||
} | ||
|
||
public class ParentViewModel21434 | ||
{ | ||
public string Text => "ParentText"; | ||
public ChildViewModel21434 Child { get; } = new(); | ||
} | ||
|
||
public class ChildViewModel21434 | ||
{ | ||
public string Text => "ChildText"; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?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.Maui21434" | ||
x:DataType="local:ParentViewModel21434"> | ||
<ContentPage.BindingContext> | ||
<local:ParentViewModel21434 /> | ||
</ContentPage.BindingContext> | ||
|
||
<VerticalStackLayout> | ||
<Label Text="{Binding Text}" x:Name="ParentTextLabel" /> | ||
<Label BindingContext="{Binding Child}" Text="{Binding Text}" x:DataType="local:ChildViewModel21434" x:Name="ChildTextLabel" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we be even smarter, and infer the x:DataType of the label ? not in all cases, but in this one it looks like we could... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we could, I opened a new issue so that we can discuss all the cases when we could infer x:DataTypes (#21834) |
||
</VerticalStackLayout> | ||
</ContentPage> |
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.
use a different property name than the one on the parent, to see if there's a compilation error (should not)
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.
The original issue had the same property name, so I stuck to that in the test. Before the fix when the property names were equal there was no compilation error, but the value displayed in the Label was incorrect (there was a mismatch between the
TSource
of the binding and the context object type it was applied with). It might be worth testing both cases.