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

Commit

Permalink
Add support for XF.GridLength in attribute helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Eilon committed May 20, 2020
1 parent 6a49cb9 commit a388f7c
Showing 1 changed file with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Helper method to serialize <see cref="GridLength" /> objects.
/// </summary>
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);
}

/// <summary>
/// Helper method to deserialize <see cref="GridLength" /> objects.
/// </summary>
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));
}
}
}

0 comments on commit a388f7c

Please sign in to comment.