diff options
author | Stephane Delcroix <stephane@delcroix.org> | 2016-09-26 22:29:47 +0200 |
---|---|---|
committer | Jason Smith <jason.smith@xamarin.com> | 2016-09-26 13:29:47 -0700 |
commit | 55f066584c507ec92d5054fac4f3a35f54c05522 (patch) | |
tree | 358ec7ed9a74e008b3bc601a9b391bcd193fb86b /Xamarin.Forms.Core | |
parent | e6a20ddedb6c8dee989b4ac19c6e83cecafe9f29 (diff) | |
download | xamarin-forms-55f066584c507ec92d5054fac4f3a35f54c05522.tar.gz xamarin-forms-55f066584c507ec92d5054fac4f3a35f54c05522.tar.bz2 xamarin-forms-55f066584c507ec92d5054fac4f3a35f54c05522.zip |
[XamlC] Compiled converters (#358)
Diffstat (limited to 'Xamarin.Forms.Core')
-rw-r--r-- | Xamarin.Forms.Core/BindablePropertyConverter.cs | 15 | ||||
-rw-r--r-- | Xamarin.Forms.Core/BindingTypeConverter.cs | 1 | ||||
-rw-r--r-- | Xamarin.Forms.Core/BoundsTypeConverter.cs | 5 | ||||
-rw-r--r-- | Xamarin.Forms.Core/ColorTypeConverter.cs | 72 | ||||
-rw-r--r-- | Xamarin.Forms.Core/LayoutOptionsConverter.cs | 24 | ||||
-rw-r--r-- | Xamarin.Forms.Core/ParameterAttribute.cs | 2 | ||||
-rw-r--r-- | Xamarin.Forms.Core/ProvideCompiledAttribute.cs | 15 | ||||
-rw-r--r-- | Xamarin.Forms.Core/RectangleTypeConverter.cs | 1 | ||||
-rw-r--r-- | Xamarin.Forms.Core/TypeConverter.cs | 2 | ||||
-rw-r--r-- | Xamarin.Forms.Core/Xamarin.Forms.Core.csproj | 1 |
10 files changed, 72 insertions, 66 deletions
diff --git a/Xamarin.Forms.Core/BindablePropertyConverter.cs b/Xamarin.Forms.Core/BindablePropertyConverter.cs index af3fd5b6..a7398e08 100644 --- a/Xamarin.Forms.Core/BindablePropertyConverter.cs +++ b/Xamarin.Forms.Core/BindablePropertyConverter.cs @@ -7,6 +7,7 @@ using Xamarin.Forms.Xaml; namespace Xamarin.Forms { + [Xaml.ProvideCompiled("Xamarin.Forms.Core.XamlC.BindablePropertyConverter")] public sealed class BindablePropertyConverter : TypeConverter, IExtendedTypeConverter { object IExtendedTypeConverter.ConvertFrom(CultureInfo culture, object value, IServiceProvider serviceProvider) @@ -51,10 +52,7 @@ namespace Xamarin.Forms type = (parentValuesProvider.TargetObject as Trigger).TargetType; if (type == null) - { - string msg = string.Format("Can't resolve {0}", parts[0]); - throw new XamlParseException(msg, lineinfo); - } + throw new XamlParseException($"Can't resolve {parts [0]}", lineinfo); return ConvertFrom(type, parts[0], lineinfo); } @@ -67,8 +65,7 @@ namespace Xamarin.Forms } return ConvertFrom(type, parts[1], lineinfo); } - string emsg = string.Format("Can't resolve {0}. Syntax is [[ns:]Type.]PropertyName.", value); - throw new XamlParseException(emsg, lineinfo); + throw new XamlParseException($"Can't resolve {value}. Syntax is [[prefix:]Type.]PropertyName.", lineinfo); } public override object ConvertFromInvariantString(string value) @@ -83,7 +80,7 @@ namespace Xamarin.Forms string[] parts = value.Split('.'); if (parts.Length != 2) { - Log.Warning(null, "Can't resolve {0}. Accepted syntax is Type.PropertyName.", value); + Log.Warning(null, $"Can't resolve {value}. Accepted syntax is Type.PropertyName."); return null; } Type type = Type.GetType("Xamarin.Forms." + parts[0]); @@ -95,10 +92,10 @@ namespace Xamarin.Forms string name = propertyName + "Property"; FieldInfo bpinfo = type.GetField(fi => fi.Name == name && fi.IsStatic && fi.IsPublic && fi.FieldType == typeof(BindableProperty)); if (bpinfo == null) - throw new XamlParseException(string.Format("Can't resolve {0} on {1}", name, type.Name), lineinfo); + throw new XamlParseException($"Can't resolve {name} on {type.Name}", lineinfo); var bp = bpinfo.GetValue(null) as BindableProperty; if (bp.PropertyName != propertyName) - throw new XamlParseException(string.Format("The PropertyName of {0}.{1} is not {2}", type.Name, name, propertyName), lineinfo); + throw new XamlParseException($"The PropertyName of {type.Name}.{name} is not {propertyName}", lineinfo); return bp; } } diff --git a/Xamarin.Forms.Core/BindingTypeConverter.cs b/Xamarin.Forms.Core/BindingTypeConverter.cs index 07dda896..2e57ac88 100644 --- a/Xamarin.Forms.Core/BindingTypeConverter.cs +++ b/Xamarin.Forms.Core/BindingTypeConverter.cs @@ -1,5 +1,6 @@ namespace Xamarin.Forms { + [Xaml.ProvideCompiled("Xamarin.Forms.Core.XamlC.BindingTypeConverter")] public sealed class BindingTypeConverter : TypeConverter { public override object ConvertFromInvariantString(string value) diff --git a/Xamarin.Forms.Core/BoundsTypeConverter.cs b/Xamarin.Forms.Core/BoundsTypeConverter.cs index 549b7b63..68464f8e 100644 --- a/Xamarin.Forms.Core/BoundsTypeConverter.cs +++ b/Xamarin.Forms.Core/BoundsTypeConverter.cs @@ -3,7 +3,8 @@ using System.Globalization; namespace Xamarin.Forms { - public class BoundsTypeConverter : TypeConverter + [Xaml.ProvideCompiled ("Xamarin.Forms.Core.XamlC.BoundsTypeConverter")] + public sealed class BoundsTypeConverter : TypeConverter { public override object ConvertFromInvariantString(string value) { @@ -36,7 +37,7 @@ namespace Xamarin.Forms return new Rectangle(x, y, w, h); } - throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", value, typeof(Rectangle))); + throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(Rectangle)}"); } } }
\ No newline at end of file diff --git a/Xamarin.Forms.Core/ColorTypeConverter.cs b/Xamarin.Forms.Core/ColorTypeConverter.cs index 612174e9..0eb6fab3 100644 --- a/Xamarin.Forms.Core/ColorTypeConverter.cs +++ b/Xamarin.Forms.Core/ColorTypeConverter.cs @@ -1,9 +1,9 @@ using System; using System.Linq; -using System.Reflection; namespace Xamarin.Forms { + [Xaml.ProvideCompiled("Xamarin.Forms.Core.XamlC.ColorTypeConverter")] public class ColorTypeConverter : TypeConverter { public override object ConvertFromInvariantString(string value) @@ -16,59 +16,39 @@ namespace Xamarin.Forms if (parts.Length == 1 || (parts.Length == 2 && parts[0] == "Color")) { string color = parts[parts.Length - 1]; - switch (color) - { - case "Default": - return Color.Default; - case "Transparent": - return Color.Transparent; - case "Aqua": - return Color.Aqua; - case "Black": - return Color.Black; - case "Blue": - return Color.Blue; - case "Fuchsia": - return Color.Fuchsia; - case "Gray": - return Color.Gray; - case "Green": - return Color.Green; - case "Lime": - return Color.Lime; - case "Maroon": - return Color.Maroon; - case "Navy": - return Color.Navy; - case "Olive": - return Color.Olive; - case "Orange": - return Color.Orange; - case "Purple": - return Color.Purple; - case "Pink": - return Color.Pink; - case "Red": - return Color.Red; - case "Silver": - return Color.Silver; - case "Teal": - return Color.Teal; - case "White": - return Color.White; - case "Yellow": - return Color.Yellow; + switch (color) { + case "Default": return Color.Default; + case "Accent": return Color.Accent; + case "Transparent": return Color.Transparent; + case "Aqua": return Color.Aqua; + case "Black": return Color.Black; + case "Blue": return Color.Blue; + case "Fuchsia": return Color.Fuchsia; + case "Gray": return Color.Gray; + case "Green": return Color.Green; + case "Lime": return Color.Lime; + case "Maroon": return Color.Maroon; + case "Navy": return Color.Navy; + case "Olive": return Color.Olive; + case "Orange": return Color.Orange; + case "Purple": return Color.Purple; + case "Pink": return Color.Pink; + case "Red": return Color.Red; + case "Silver": return Color.Silver; + case "Teal": return Color.Teal; + case "White": return Color.White; + case "Yellow": return Color.Yellow; } - FieldInfo field = typeof(Color).GetFields().FirstOrDefault(fi => fi.IsStatic && fi.Name == color); + var field = typeof(Color).GetFields().FirstOrDefault(fi => fi.IsStatic && fi.Name == color); if (field != null) return (Color)field.GetValue(null); - PropertyInfo property = typeof(Color).GetProperties().FirstOrDefault(pi => pi.Name == color && pi.CanRead && pi.GetMethod.IsStatic); + var property = typeof(Color).GetProperties().FirstOrDefault(pi => pi.Name == color && pi.CanRead && pi.GetMethod.IsStatic); if (property != null) return (Color)property.GetValue(null, null); } } - throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", value, typeof(Color))); + throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(Color)}"); } } }
\ No newline at end of file diff --git a/Xamarin.Forms.Core/LayoutOptionsConverter.cs b/Xamarin.Forms.Core/LayoutOptionsConverter.cs index 746e56ce..6dae0f02 100644 --- a/Xamarin.Forms.Core/LayoutOptionsConverter.cs +++ b/Xamarin.Forms.Core/LayoutOptionsConverter.cs @@ -4,22 +4,32 @@ using System.Reflection; namespace Xamarin.Forms { + [Xaml.ProvideCompiled("Xamarin.Forms.Core.XamlC.LayoutOptionsConverter")] public sealed class LayoutOptionsConverter : TypeConverter { public override object ConvertFromInvariantString(string value) { - if (value != null) - { - string[] parts = value.Split('.'); - if (parts.Length > 2 || (parts.Length == 2 && parts[0] != "LayoutOptions")) - throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", value, typeof(LayoutOptions))); - value = parts[parts.Length - 1]; + if (value != null) { + var parts = value.Split('.'); + if (parts.Length > 2 || (parts.Length == 2 && parts [0] != "LayoutOptions")) + throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(LayoutOptions)}"); + value = parts [parts.Length - 1]; + switch (value) { + case "Start": return LayoutOptions.Start; + case "Center": return LayoutOptions.Center; + case "End": return LayoutOptions.End; + case "Fill": return LayoutOptions.Fill; + case "StartAndExpand": return LayoutOptions.StartAndExpand; + case "CenterAndExpand": return LayoutOptions.CenterAndExpand; + case "EndAndExpand": return LayoutOptions.EndAndExpand; + case "FillAndExpand": return LayoutOptions.FillAndExpand; + } FieldInfo field = typeof(LayoutOptions).GetFields().FirstOrDefault(fi => fi.IsStatic && fi.Name == value); if (field != null) return (LayoutOptions)field.GetValue(null); } - throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", value, typeof(LayoutOptions))); + throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(LayoutOptions)}"); } } }
\ No newline at end of file diff --git a/Xamarin.Forms.Core/ParameterAttribute.cs b/Xamarin.Forms.Core/ParameterAttribute.cs index d8a07267..629a454f 100644 --- a/Xamarin.Forms.Core/ParameterAttribute.cs +++ b/Xamarin.Forms.Core/ParameterAttribute.cs @@ -10,6 +10,6 @@ namespace Xamarin.Forms Name = name; } - public string Name { get; set; } + public string Name { get; } } }
\ No newline at end of file diff --git a/Xamarin.Forms.Core/ProvideCompiledAttribute.cs b/Xamarin.Forms.Core/ProvideCompiledAttribute.cs new file mode 100644 index 00000000..f8e261ba --- /dev/null +++ b/Xamarin.Forms.Core/ProvideCompiledAttribute.cs @@ -0,0 +1,15 @@ +using System; + +namespace Xamarin.Forms.Xaml +{ + [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)] + sealed class ProvideCompiledAttribute : Attribute + { + public string CompiledVersion { get; } + + public ProvideCompiledAttribute (string compiledVersion) + { + CompiledVersion = compiledVersion; + } + } +}
\ No newline at end of file diff --git a/Xamarin.Forms.Core/RectangleTypeConverter.cs b/Xamarin.Forms.Core/RectangleTypeConverter.cs index b17a8c26..421c8060 100644 --- a/Xamarin.Forms.Core/RectangleTypeConverter.cs +++ b/Xamarin.Forms.Core/RectangleTypeConverter.cs @@ -3,6 +3,7 @@ using System.Globalization; namespace Xamarin.Forms { + [Xaml.ProvideCompiled("Xamarin.Forms.Core.XamlC.RectangleTypeConverter")] public class RectangleTypeConverter : TypeConverter { public override object ConvertFromInvariantString(string value) diff --git a/Xamarin.Forms.Core/TypeConverter.cs b/Xamarin.Forms.Core/TypeConverter.cs index 57ea3b4c..52b14d4e 100644 --- a/Xamarin.Forms.Core/TypeConverter.cs +++ b/Xamarin.Forms.Core/TypeConverter.cs @@ -8,7 +8,7 @@ namespace Xamarin.Forms public virtual bool CanConvertFrom(Type sourceType) { if (sourceType == null) - throw new ArgumentNullException("sourceType"); + throw new ArgumentNullException(nameof(sourceType)); return sourceType == typeof(string); } diff --git a/Xamarin.Forms.Core/Xamarin.Forms.Core.csproj b/Xamarin.Forms.Core/Xamarin.Forms.Core.csproj index a07b88ca..90578e3c 100644 --- a/Xamarin.Forms.Core/Xamarin.Forms.Core.csproj +++ b/Xamarin.Forms.Core/Xamarin.Forms.Core.csproj @@ -435,6 +435,7 @@ <Compile Include="NativeBindingHelpers.cs" /> <Compile Include="INativeValueConverterService.cs" /> <Compile Include="INativeBindingService.cs" /> + <Compile Include="ProvideCompiledAttribute.cs" /> </ItemGroup> <Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" /> <ItemGroup> |