summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/BoundsTypeConverter.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Core/BoundsTypeConverter.cs')
-rw-r--r--Xamarin.Forms.Core/BoundsTypeConverter.cs42
1 files changed, 42 insertions, 0 deletions
diff --git a/Xamarin.Forms.Core/BoundsTypeConverter.cs b/Xamarin.Forms.Core/BoundsTypeConverter.cs
new file mode 100644
index 00000000..549b7b63
--- /dev/null
+++ b/Xamarin.Forms.Core/BoundsTypeConverter.cs
@@ -0,0 +1,42 @@
+using System;
+using System.Globalization;
+
+namespace Xamarin.Forms
+{
+ public class BoundsTypeConverter : TypeConverter
+ {
+ public override object ConvertFromInvariantString(string value)
+ {
+ if (value != null)
+ {
+ double x = -1, y = -1, w = -1, h = -1;
+ string[] xywh = value.Split(',');
+ bool hasX, hasY, hasW, hasH;
+
+ hasX = (xywh.Length == 2 || xywh.Length == 4) && double.TryParse(xywh[0], NumberStyles.Number, CultureInfo.InvariantCulture, out x);
+ hasY = (xywh.Length == 2 || xywh.Length == 4) && double.TryParse(xywh[1], NumberStyles.Number, CultureInfo.InvariantCulture, out y);
+ hasW = xywh.Length == 4 && double.TryParse(xywh[2], NumberStyles.Number, CultureInfo.InvariantCulture, out w);
+ hasH = xywh.Length == 4 && double.TryParse(xywh[3], NumberStyles.Number, CultureInfo.InvariantCulture, out h);
+
+ if (!hasW && xywh.Length == 4 && string.Compare("AutoSize", xywh[2].Trim(), StringComparison.OrdinalIgnoreCase) == 0)
+ {
+ hasW = true;
+ w = AbsoluteLayout.AutoSize;
+ }
+
+ if (!hasH && xywh.Length == 4 && string.Compare("AutoSize", xywh[3].Trim(), StringComparison.OrdinalIgnoreCase) == 0)
+ {
+ hasH = true;
+ h = AbsoluteLayout.AutoSize;
+ }
+
+ if (hasX && hasY && xywh.Length == 2)
+ return new Rectangle(x, y, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize);
+ if (hasX && hasY && hasW && hasH && xywh.Length == 4)
+ return new Rectangle(x, y, w, h);
+ }
+
+ throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", value, typeof(Rectangle)));
+ }
+ }
+} \ No newline at end of file