From a388f7cc0b3599c1cd7e4a1cf35492dad357a8bf Mon Sep 17 00:00:00 2001 From: Eilon Lipton Date: Wed, 20 May 2020 16:31:59 -0700 Subject: [PATCH] Add support for XF.GridLength in attribute helpers --- .../Elements/AttributeHelper.GridLength.cs | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/Microsoft.MobileBlazorBindings/Elements/AttributeHelper.GridLength.cs diff --git a/src/Microsoft.MobileBlazorBindings/Elements/AttributeHelper.GridLength.cs b/src/Microsoft.MobileBlazorBindings/Elements/AttributeHelper.GridLength.cs new file mode 100644 index 00000000..307df4ca --- /dev/null +++ b/src/Microsoft.MobileBlazorBindings/Elements/AttributeHelper.GridLength.cs @@ -0,0 +1,51 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +using System; +using System.Globalization; +using Xamarin.Forms; + +namespace Microsoft.MobileBlazorBindings.Elements +{ + public static partial class AttributeHelper + { + /// + /// Helper method to serialize objects. + /// + public static string GridLengthToString(GridLength gridLength) + { + return string.Format( + CultureInfo.InvariantCulture, + "{0:R},{1}", // "R" --> Round-trip format specifier guarantees fidelity when parsing System.Double + gridLength.Value, + (int)gridLength.GridUnitType); + } + + /// + /// Helper method to deserialize objects. + /// + public static GridLength StringToGridLength(object gridLengthString, GridLength defaultValueIfNull = default) + { + if (gridLengthString is null) + { + return defaultValueIfNull; + } + if (!(gridLengthString is string gridLengthAsString)) + { + throw new ArgumentNullException(nameof(gridLengthString)); + } + + var stringParts = gridLengthAsString.Split(','); + + if (stringParts.Length != 2) + { + throw new ArgumentNullException(nameof(gridLengthString), message: "Expected value to have one comma (',') in it."); + } + + return + new GridLength( + double.Parse(stringParts[0], CultureInfo.InvariantCulture), + (GridUnitType)int.Parse(stringParts[1], CultureInfo.InvariantCulture)); + } + } +}