Skip to content
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

[release/9.0.1xx] [X] Delay binding context type check until relative binding source is resolved #25614

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions src/Controls/src/Core/Binding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,6 @@ internal override void Apply(object context, BindableObject bindObj, BindablePro
var isApplied = IsApplied;

var bindingContext = src ?? Context ?? context;

// Do not check type mismatch if this is a binding with Source and compilation of bindings with Source is disabled
bool skipTypeMismatchCheck = Source is not null && !RuntimeFeature.IsXamlCBindingWithSourceCompilationEnabled;
if (!skipTypeMismatchCheck)
{
if (DataType != null && bindingContext != null && !DataType.IsAssignableFrom(bindingContext.GetType()))
{
BindingDiagnostics.SendBindingFailure(this, "Binding", $"Mismatch between the specified x:DataType ({DataType}) and the current binding context ({bindingContext.GetType()}).");
bindingContext = null;
}
}

base.Apply(bindingContext, bindObj, targetProperty, fromBindingContextChanged, specificity);

if (src != null && isApplied && fromBindingContextChanged)
Expand Down
14 changes: 14 additions & 0 deletions src/Controls/src/Core/BindingExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ internal void Apply(bool fromTarget = false)
/// </summary>
internal void Apply(object sourceObject, BindableObject target, BindableProperty property, SetterSpecificity specificity)
{
if (Binding is Binding { Source: var source, DataType: Type dataType })
{
// Do not check type mismatch if this is a binding with Source and compilation of bindings with Source is disale
bool skipTypeMismatchCheck = source is not null && !RuntimeFeature.IsXamlCBindingWithSourceCompilationEnabled;
if (!skipTypeMismatchCheck)
{
if (sourceObject != null && !dataType.IsAssignableFrom(sourceObject.GetType()))
{
BindingDiagnostics.SendBindingFailure(Binding, "Binding", $"Mismatch between the specified x:DataType ({dataType}) and the current binding context ({sourceObject.GetType()}).");
sourceObject = null;
}
}
}

_targetProperty = property;
_specificity = specificity;

Expand Down
11 changes: 11 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui25608.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?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"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui25608">
<VerticalStackLayout Spacing="25">
<Image
x:Name="Image"
Source="dotnet_bot.png"
HeightRequest="{Binding Spacing, x:DataType=VerticalStackLayout, Source={RelativeSource AncestorType={x:Type VerticalStackLayout}}}" />
</VerticalStackLayout>
</ContentPage>
60 changes: 60 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui25608.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Dispatching;

using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.Controls.Xaml.Diagnostics;
using Microsoft.Maui.UnitTests;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.Xaml.UnitTests;

public partial class Maui25608
{
public Maui25608()
{
InitializeComponent();
}

public Maui25608(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}

[TestFixture]
class Test
{
EventHandler<BindingBaseErrorEventArgs> _bindingFailureHandler;

[SetUp]
public void Setup()
{
Application.SetCurrentApplication(new MockApplication());
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
}

[TearDown]
public void TearDown()
{
if (_bindingFailureHandler is not null)
{
BindingDiagnostics.BindingFailed -= _bindingFailureHandler;
}

AppInfo.SetCurrent(null);
}

[Test]
public void TestNonCompiledResourceDictionary([Values(false, true)] bool useCompiledXaml)
{
bool bindingFailureReported = false;
_bindingFailureHandler = (sender, args) => bindingFailureReported = true;
BindingDiagnostics.BindingFailed += _bindingFailureHandler;

var page = new Maui25608(useCompiledXaml);

Assert.AreEqual(25, page.Image.HeightRequest);
Assert.IsFalse(bindingFailureReported);
}
}
}