Skip to content

Commit

Permalink
[X] avoid AmibuousMatchException
Browse files Browse the repository at this point in the history
with XamlC disabled, setting an overriden property throws a
AmbiguousMatchException. This fixes the mismatch between the 2
inflaters.

- fixes #13962
  • Loading branch information
StephaneDelcroix authored and jfversluis committed Aug 15, 2023
1 parent 844b019 commit 59e6a4d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
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);
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);
}
}
}

0 comments on commit 59e6a4d

Please sign in to comment.