This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for XF.GridLength in attribute helpers
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/Microsoft.MobileBlazorBindings/Elements/AttributeHelper.GridLength.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |