Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
[C, Xaml] add SizeTypeConverter
Browse files Browse the repository at this point in the history
Add a missing TypeConverter attribute to Size struct.

- fixes #3280
  • Loading branch information
StephaneDelcroix committed Jul 26, 2018
1 parent d675b3e commit e6d8dca
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 4 deletions.
8 changes: 5 additions & 3 deletions Xamarin.Forms.Core/RectangleTypeConverter.cs
Expand Up @@ -11,10 +11,12 @@ public override object ConvertFromInvariantString(string value)
{
if (value != null)
{
double x, y, w, h;
string[] xywh = value.Split(',');
if (xywh.Length == 4 && double.TryParse(xywh[0], NumberStyles.Number, CultureInfo.InvariantCulture, out x) && double.TryParse(xywh[1], NumberStyles.Number, CultureInfo.InvariantCulture, out y) &&
double.TryParse(xywh[2], NumberStyles.Number, CultureInfo.InvariantCulture, out w) && double.TryParse(xywh[3], NumberStyles.Number, CultureInfo.InvariantCulture, out h))
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 Rectangle(x, y, w, h);
}

Expand Down
1 change: 1 addition & 0 deletions Xamarin.Forms.Core/Size.cs
Expand Up @@ -6,6 +6,7 @@
namespace Xamarin.Forms
{
[DebuggerDisplay("Width={Width}, Height={Height}")]
[TypeConverter(typeof(SizeTypeConverter))]
public struct Size
{
double _width;
Expand Down
22 changes: 22 additions & 0 deletions Xamarin.Forms.Core/SizeTypeConverter.cs
@@ -0,0 +1,22 @@
using System;
using System.Globalization;

namespace Xamarin.Forms
{
[Xaml.TypeConversion(typeof(Size))]
public class SizeTypeConverter : TypeConverter
{
public override object ConvertFromInvariantString(string value)
{
if (value != null) {
string[] wh = value.Split(',');
if (wh.Length == 2
&& double.TryParse(wh[0], NumberStyles.Number, CultureInfo.InvariantCulture, out double w)
&& double.TryParse(wh[1], NumberStyles.Number, CultureInfo.InvariantCulture, out double h))
return new Size(w, h);
}

throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", value, typeof(Size)));
}
}
}
3 changes: 3 additions & 0 deletions Xamarin.Forms.Xaml.UnitTests/Issues/Gh3280.xaml
@@ -0,0 +1,3 @@
<?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.Gh3280" Foo="15,25">
</ContentPage>
48 changes: 48 additions & 0 deletions Xamarin.Forms.Xaml.UnitTests/Issues/Gh3280.xaml.cs
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xamarin.Forms;
using Xamarin.Forms.Core.UnitTests;

namespace Xamarin.Forms.Xaml.UnitTests
{
public partial class Gh3280 : ContentPage
{
public Gh3280()
{
InitializeComponent();
}

public Size Foo { get; set; }

public Gh3280(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;
}

[TestCase(false), TestCase(true)]
public void SizeHasConverter(bool useCompiledXaml)
{
Gh3280 layout = null;
Assert.DoesNotThrow(() => layout = new Gh3280(useCompiledXaml));
Assert.That(layout.Foo, Is.EqualTo(new Size(15, 25)));
}
}
}
}
Expand Up @@ -631,6 +631,9 @@
<Compile Include="Issues\Gh3260.xaml.cs">
<DependentUpon>Gh3260.xaml</DependentUpon>
</Compile>
<Compile Include="Issues\Gh3280.xaml.cs">
<DependentUpon>Gh3280.xaml</DependentUpon>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="..\.nuspec\Xamarin.Forms.Debug.targets" Condition="'$(BuildingInsideVisualStudio)' == 'true' AND Exists('..\.nuspec\Xamarin.Forms.Build.Tasks.dll')" />
Expand Down Expand Up @@ -1145,6 +1148,10 @@
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="Issues\Gh3280.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
Expand Down Expand Up @@ -1198,4 +1205,4 @@
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
</Project>
</Project>

0 comments on commit e6d8dca

Please sign in to comment.