From 59e6a4da335390e127d0b67685e90ca225f3e673 Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Tue, 27 Jun 2023 12:26:13 +0200 Subject: [PATCH] [X] avoid AmibuousMatchException with XamlC disabled, setting an overriden property throws a AmbiguousMatchException. This fixes the mismatch between the 2 inflaters. - fixes #13962 --- .../src/Xaml/ApplyPropertiesVisitor.cs | 12 +---- .../Xaml.UnitTests/Issues/Maui13962.xaml | 7 +++ .../Xaml.UnitTests/Issues/Maui13962.xaml.cs | 44 +++++++++++++++++++ 3 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 src/Controls/tests/Xaml.UnitTests/Issues/Maui13962.xaml create mode 100644 src/Controls/tests/Xaml.UnitTests/Issues/Maui13962.xaml.cs diff --git a/src/Controls/src/Xaml/ApplyPropertiesVisitor.cs b/src/Controls/src/Xaml/ApplyPropertiesVisitor.cs index 0f3ac4d42b54..55ce7021c5ad 100644 --- a/src/Controls/src/Xaml/ApplyPropertiesVisitor.cs +++ b/src/Controls/src/Xaml/ApplyPropertiesVisitor.cs @@ -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); var convertedValue = value.ConvertTo(property.ReturnType, minforetriever, serviceProvider, out exception); if (exception != null) return false; diff --git a/src/Controls/tests/Xaml.UnitTests/Issues/Maui13962.xaml b/src/Controls/tests/Xaml.UnitTests/Issues/Maui13962.xaml new file mode 100644 index 000000000000..90a06fec8b22 --- /dev/null +++ b/src/Controls/tests/Xaml.UnitTests/Issues/Maui13962.xaml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/src/Controls/tests/Xaml.UnitTests/Issues/Maui13962.xaml.cs b/src/Controls/tests/Xaml.UnitTests/Issues/Maui13962.xaml.cs new file mode 100644 index 000000000000..c670644cfbb9 --- /dev/null +++ b/src/Controls/tests/Xaml.UnitTests/Issues/Maui13962.xaml.cs @@ -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); + } + } +} \ No newline at end of file