From 3e0ee965d710768f1c434b469f8c1c8371b7659a Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Tue, 27 Sep 2016 20:10:39 +0200 Subject: [Xaml] more primitive types (#385) * [Xaml] more builtin conversion, and more type primitives * [XamlC] more builtin conversion, more type primitives --- Xamarin.Forms.Xaml/CreateValuesVisitor.cs | 108 ++++++++++++++----------- Xamarin.Forms.Xaml/TypeConversionExtensions.cs | 34 ++++++-- 2 files changed, 88 insertions(+), 54 deletions(-) (limited to 'Xamarin.Forms.Xaml') diff --git a/Xamarin.Forms.Xaml/CreateValuesVisitor.cs b/Xamarin.Forms.Xaml/CreateValuesVisitor.cs index 25a936fa..c2331bf1 100644 --- a/Xamarin.Forms.Xaml/CreateValuesVisitor.cs +++ b/Xamarin.Forms.Xaml/CreateValuesVisitor.cs @@ -296,73 +296,87 @@ namespace Xamarin.Forms.Xaml { var valuestring = ((ValueNode)node.CollectionItems[0]).Value as string; - if (nodeType == typeof (bool)) - { - bool outbool; - if (bool.TryParse(valuestring, out outbool)) - value = outbool; + if (nodeType == typeof(SByte)) { + sbyte retval; + if (sbyte.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval)) + return retval; } - else if (nodeType == typeof (char)) - { - char retval; - if (char.TryParse(valuestring, out retval)) - value = retval; + if (nodeType == typeof(Int16)) { + short retval; + if (short.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval)) + return retval; } - else if (nodeType == typeof (string)) - value = valuestring; - else if (nodeType == typeof (decimal)) - { - decimal retval; - if (decimal.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval)) - value = retval; + if (nodeType == typeof(Int32)) { + int retval; + if (int.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval)) + return retval; } - else if (nodeType == typeof (float)) - { + if (nodeType == typeof(Int64)) { + long retval; + if (long.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval)) + return retval; + } + if (nodeType == typeof(Byte)) { + byte retval; + if (byte.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval)) + return retval; + } + if (nodeType == typeof(UInt16)) { + ushort retval; + if (ushort.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval)) + return retval; + } + if (nodeType == typeof(UInt32)) { + uint retval; + if (uint.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval)) + return retval; + } + if (nodeType == typeof(UInt64)) { + ulong retval; + if (ulong.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval)) + return retval; + } + if (nodeType == typeof(Single)) { float retval; if (float.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval)) - value = retval; + return retval; } - else if (nodeType == typeof (double)) - { + if (nodeType == typeof(Double)) { double retval; if (double.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval)) - value = retval; - } - else if (nodeType == typeof (byte)) - { - byte retval; - if (byte.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval)) - value = retval; + return retval; } - else if (nodeType == typeof (short)) + if (nodeType == typeof (Boolean)) { - short retval; - if (short.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval)) - value = retval; + bool outbool; + if (bool.TryParse(valuestring, out outbool)) + return outbool; } - else if (nodeType == typeof (int)) - { - int retval; - if (int.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval)) - value = retval; + if (nodeType == typeof(TimeSpan)) { + TimeSpan retval; + if (TimeSpan.TryParse(valuestring, CultureInfo.InvariantCulture, out retval)) + return retval; } - else if (nodeType == typeof (long)) + if (nodeType == typeof (char)) { - long retval; - if (long.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval)) - value = retval; + char retval; + if (char.TryParse(valuestring, out retval)) + return retval; } - else if (nodeType == typeof (TimeSpan)) + if (nodeType == typeof (string)) + return valuestring; + if (nodeType == typeof (decimal)) { - TimeSpan retval; - if (TimeSpan.TryParse(valuestring, CultureInfo.InvariantCulture, out retval)) - value = retval; + decimal retval; + if (decimal.TryParse(valuestring, NumberStyles.Number, CultureInfo.InvariantCulture, out retval)) + return retval; } + else if (nodeType == typeof (Uri)) { Uri retval; if (Uri.TryCreate(valuestring, UriKind.RelativeOrAbsolute, out retval)) - value = retval; + return retval; } } return value; diff --git a/Xamarin.Forms.Xaml/TypeConversionExtensions.cs b/Xamarin.Forms.Xaml/TypeConversionExtensions.cs index 7e377ea4..a29b3af2 100644 --- a/Xamarin.Forms.Xaml/TypeConversionExtensions.cs +++ b/Xamarin.Forms.Xaml/TypeConversionExtensions.cs @@ -130,23 +130,43 @@ namespace Xamarin.Forms.Xaml //Obvious Built-in conversions if (toType.GetTypeInfo().IsEnum) return Enum.Parse(toType, str); - //TODO supports Int16, 64, Byte, Char, ... - if (toType == typeof (Int32)) + if (toType == typeof(SByte)) + return SByte.Parse(str, CultureInfo.InvariantCulture); + if (toType == typeof(Int16)) + return Int16.Parse(str, CultureInfo.InvariantCulture); + if (toType == typeof(Int32)) return Int32.Parse(str, CultureInfo.InvariantCulture); - if (toType == typeof (float)) + if (toType == typeof(Int64)) + return Int64.Parse(str, CultureInfo.InvariantCulture); + if (toType == typeof(Byte)) + return Byte.Parse(str, CultureInfo.InvariantCulture); + if (toType == typeof(UInt16)) + return UInt16.Parse(str, CultureInfo.InvariantCulture); + if (toType == typeof(UInt32)) + return UInt32.Parse(str, CultureInfo.InvariantCulture); + if (toType == typeof(UInt64)) + return UInt64.Parse(str, CultureInfo.InvariantCulture); + if (toType == typeof (Single)) return Single.Parse(str, CultureInfo.InvariantCulture); - if (toType == typeof (double)) + if (toType == typeof (Double)) return Double.Parse(str, CultureInfo.InvariantCulture); - if (toType == typeof (bool)) + if (toType == typeof (Boolean)) return Boolean.Parse(str); if (toType == typeof (TimeSpan)) return TimeSpan.Parse(str, CultureInfo.InvariantCulture); if (toType == typeof (DateTime)) return DateTime.Parse(str, CultureInfo.InvariantCulture); - if (toType == typeof (string) && str.StartsWith("{}", StringComparison.Ordinal)) + if (toType == typeof(Char)) { + char c = '\0'; + Char.TryParse(str, out c); + return c; + } + if (toType == typeof (String) && str.StartsWith("{}", StringComparison.Ordinal)) return str.Substring(2); - if (toType == typeof (string)) + if (toType == typeof (String)) return value; + if (toType == typeof(Decimal)) + return Decimal.Parse(str, CultureInfo.InvariantCulture); } //if there's an implicit conversion, convert -- cgit v1.2.3