From 68aae689cf13573e1ec260c27ea984f41655d074 Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Fri, 24 Jul 2020 17:21:34 +0200 Subject: [PATCH] [Shapes] Fix RectTypeConverter Fix inheritance of RectTypeConverter from RectangleTypeConverter, as the child isn't precompiled, even if the parent has the attribute... - fixes #11541 --- Xamarin.Forms.Core/RectTypeConverter.cs | 21 +++++++++--- .../Issues/Gh11541.xaml | 11 +++++++ .../Issues/Gh11541.xaml.cs | 33 +++++++++++++++++++ 3 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Gh11541.xaml create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Gh11541.xaml.cs diff --git a/Xamarin.Forms.Core/RectTypeConverter.cs b/Xamarin.Forms.Core/RectTypeConverter.cs index 47b785509a6..6d15a4bb0af 100644 --- a/Xamarin.Forms.Core/RectTypeConverter.cs +++ b/Xamarin.Forms.Core/RectTypeConverter.cs @@ -1,14 +1,25 @@ -namespace Xamarin.Forms +using System; +using System.Globalization; + +namespace Xamarin.Forms { [Xaml.TypeConversion(typeof(Rect))] - public class RectTypeConverter : RectangleTypeConverter + public class RectTypeConverter : TypeConverter { public override object ConvertFromInvariantString(string value) { - var rectangle = base.ConvertFromInvariantString(value); - Rect rect = (Rectangle)rectangle; + if (value != null) + { + string[] xywh = value.Split(','); + if (xywh.Length == 4 + && double.TryParse(xywh[0], NumberStyles.Number, CultureInfo.InvariantCulture, out double x) + && double.TryParse(xywh[1], NumberStyles.Number, CultureInfo.InvariantCulture, out double y) + && double.TryParse(xywh[2], NumberStyles.Number, CultureInfo.InvariantCulture, out double w) + && double.TryParse(xywh[3], NumberStyles.Number, CultureInfo.InvariantCulture, out double h)) + return new Rect(x, y, w, h); + } - return rect; + throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", value, typeof(Rect))); } } } \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Gh11541.xaml b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh11541.xaml new file mode 100644 index 00000000000..e37c36f37db --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh11541.xaml @@ -0,0 +1,11 @@ + + + + + + + + diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Gh11541.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh11541.xaml.cs new file mode 100644 index 00000000000..537c863ad8a --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh11541.xaml.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; + +using Xamarin.Forms; +using Xamarin.Forms.Core.UnitTests; + +using NUnit.Framework; + +namespace Xamarin.Forms.Xaml.UnitTests +{ + public partial class Gh11541 : ContentPage + { + public Gh11541() => InitializeComponent(); + public Gh11541(bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + + [TestFixture] + class Tests + { + [SetUp] public void Setup() => Device.PlatformServices = new MockPlatformServices(); + + [TearDown] public void TearDown() => Device.PlatformServices = null; + + [Test] + public void RectangleGeometryDoesntThrow([Values(false, true)] bool useCompiledXaml) + { + var layout = new Gh11541(useCompiledXaml); + } + } + } +}