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

[Shapes] Fix RectTypeConverter #11548

Merged
merged 1 commit into from Jul 25, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 16 additions & 5 deletions 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)));
}
}
}
11 changes: 11 additions & 0 deletions Xamarin.Forms.Xaml.UnitTests/Issues/Gh11541.xaml
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Xamarin.Forms.Xaml.UnitTests.Gh11541">
<Image Source="monkeyface.png">
<Image.Clip>
<RectangleGeometry Rect="40,100,300,150" />
</Image.Clip>
</Image>
</ContentPage>
33 changes: 33 additions & 0 deletions 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);
}
}
}
}