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

[X] avoid AmbiguousMatchException #15873

Merged
merged 1 commit into from
Aug 15, 2023
Merged
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: 1 addition & 11 deletions src/Controls/src/Xaml/ApplyPropertiesVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -583,17 +583,7 @@ static bool TrySetValue(object element, BindableProperty property, bool attached
}
};
else
minforetriever = () =>
{
try
{
return property.DeclaringType.GetRuntimeProperty(property.PropertyName);
}
catch (AmbiguousMatchException e)
{
throw new XamlParseException($"Multiple properties with name '{property.DeclaringType}.{property.PropertyName}' found.", lineInfo, innerException: e);
}
};
minforetriever = () => property.DeclaringType.GetRuntimeProperties().FirstOrDefault(pi => pi.Name == property.PropertyName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case anyone else is also curious, initially I was a bit unsure if using the GRP method would fix the issue of retrieving the correct property when there are multiple present. But after talking to @StephaneDelcroix offline and looking at some docs, GRP is built on top of GetProperties, and starting from .NET 7, the order in which the properties are returned is "deterministic based upon the metadata ordering in the assembly" (docs). So, using FirstOrDefault in conjunction with GRP works in our favor to get the relevant property we are seeking.

var convertedValue = value.ConvertTo(property.ReturnType, minforetriever, serviceProvider, out exception);
if (exception != null)
return false;
Expand Down
7 changes: 7 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui13962.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentView 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.Maui13962">
<local:Maui13962CustomCheckBox IsChecked="false" />
</ContentView>
44 changes: 44 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui13962.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.Controls.Shapes;
using Microsoft.Maui.Devices;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.Xaml.UnitTests;

public class Maui13962CustomCheckBox : CheckBox
{
public static new readonly BindableProperty IsCheckedProperty =
BindableProperty.Create(nameof(IsChecked), typeof(bool?), typeof(Maui13962CustomCheckBox), false, BindingMode.TwoWay);

public new bool? IsChecked
{
get { return (bool?)this.GetValue(IsCheckedProperty); }
set { this.SetValue(IsCheckedProperty, value); }
}
}

public partial class Maui13962 : ContentView
{

public Maui13962() => InitializeComponent();

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

[TestFixture]
class Test
{
[SetUp] public void Setup() => AppInfo.SetCurrent(new MockAppInfo());
[TearDown] public void TearDown() => AppInfo.SetCurrent(null);

[Test]
public void ResolutionOfOverridenBP([Values(false, true)] bool useCompiledXaml)
{
//shouln't throw
var page = new Maui13962(useCompiledXaml);
}
}
}