summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephane Delcroix <stephane@delcroix.org>2016-12-12 09:35:42 (GMT)
committerGitHub <noreply@github.com>2016-12-12 09:35:42 (GMT)
commitf0ea0fa4bf86c7b641d0ddc3c734a5b7971c90f7 (patch)
tree57536ced5b4810c2f415a03d0fb91387cd9556c4
parent1c796c3a9edfa6a5f16689e5e7ea775c8ef21339 (diff)
downloadxamarin-forms-f0ea0fa4bf86c7b641d0ddc3c734a5b7971c90f7.zip
xamarin-forms-f0ea0fa4bf86c7b641d0ddc3c734a5b7971c90f7.tar.gz
xamarin-forms-f0ea0fa4bf86c7b641d0ddc3c734a5b7971c90f7.tar.bz2
[XamlC] compile ThicknessTypeConverter (#603)
-rw-r--r--Xamarin.Forms.Build.Tasks/CompiledConverters/ThicknessTypeConverter.cs51
-rw-r--r--Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj1
-rw-r--r--Xamarin.Forms.Core/ThicknessTypeConverter.cs3
-rw-r--r--Xamarin.Forms.Xaml.UnitTests/CompiledTypeConverter.xaml4
-rw-r--r--Xamarin.Forms.Xaml.UnitTests/CompiledTypeConverter.xaml.cs3
5 files changed, 58 insertions, 4 deletions
diff --git a/Xamarin.Forms.Build.Tasks/CompiledConverters/ThicknessTypeConverter.cs b/Xamarin.Forms.Build.Tasks/CompiledConverters/ThicknessTypeConverter.cs
new file mode 100644
index 0000000..e16a8b0
--- /dev/null
+++ b/Xamarin.Forms.Build.Tasks/CompiledConverters/ThicknessTypeConverter.cs
@@ -0,0 +1,51 @@
+using System.Collections.Generic;
+using System.Globalization;
+using System.Linq;
+
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+
+using Xamarin.Forms.Xaml;
+
+namespace Xamarin.Forms.Core.XamlC
+{
+ class ThicknessTypeConverter : ICompiledTypeConverter
+ {
+ public IEnumerable<Instruction> ConvertFromString(string value, ModuleDefinition module, BaseNode node)
+ {
+ if (!string.IsNullOrEmpty(value)) {
+ double l, t, r, b;
+ var thickness = value.Split(',');
+ switch (thickness.Length) {
+ case 1:
+ if (double.TryParse(thickness[0], NumberStyles.Number, CultureInfo.InvariantCulture, out l))
+ return GenerateIL(module, l);
+ break;
+ case 2:
+ if (double.TryParse(thickness[0], NumberStyles.Number, CultureInfo.InvariantCulture, out l) &&
+ double.TryParse(thickness[1], NumberStyles.Number, CultureInfo.InvariantCulture, out t))
+ return GenerateIL(module, l, t);
+ break;
+ case 4:
+ if (double.TryParse(thickness[0], NumberStyles.Number, CultureInfo.InvariantCulture, out l) &&
+ double.TryParse(thickness[1], NumberStyles.Number, CultureInfo.InvariantCulture, out t) &&
+ double.TryParse(thickness[2], NumberStyles.Number, CultureInfo.InvariantCulture, out r) &&
+ double.TryParse(thickness[3], NumberStyles.Number, CultureInfo.InvariantCulture, out b))
+ return GenerateIL(module, l, t, r, b);
+ break;
+ }
+ }
+ throw new XamlParseException($"Cannot convert \"{value}\" into {typeof(Thickness)}", node);
+ }
+
+ IEnumerable<Instruction> GenerateIL(ModuleDefinition module, params double[] args)
+ {
+ foreach (var d in args)
+ yield return Instruction.Create(OpCodes.Ldc_R8, d);
+ var thicknessCtor = module.Import(typeof(Thickness)).Resolve().Methods.FirstOrDefault(md => md.IsConstructor && md.Parameters.Count == args.Length);
+ var thicknessCtorRef = module.Import(thicknessCtor);
+ yield return Instruction.Create(OpCodes.Newobj, thicknessCtorRef);
+ }
+ }
+
+} \ No newline at end of file
diff --git a/Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj b/Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj
index 9507ea3..1ccace2 100644
--- a/Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj
+++ b/Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj
@@ -102,6 +102,7 @@
<Compile Include="XamlTask.cs" />
<Compile Include="CompiledMarkupExtensions\ArrayExtension.cs" />
<Compile Include="CompiledConverters\ConstraintTypeConverter.cs" />
+ <Compile Include="CompiledConverters\ThicknessTypeConverter.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Target Name="AfterBuild">
diff --git a/Xamarin.Forms.Core/ThicknessTypeConverter.cs b/Xamarin.Forms.Core/ThicknessTypeConverter.cs
index 5e79d25..fa60267 100644
--- a/Xamarin.Forms.Core/ThicknessTypeConverter.cs
+++ b/Xamarin.Forms.Core/ThicknessTypeConverter.cs
@@ -3,6 +3,7 @@ using System.Globalization;
namespace Xamarin.Forms
{
+ [Xaml.ProvideCompiled("Xamarin.Forms.Core.XamlC.ThicknessTypeConverter")]
public class ThicknessTypeConverter : TypeConverter
{
public override object ConvertFromInvariantString(string value)
@@ -29,7 +30,7 @@ namespace Xamarin.Forms
}
}
- throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", value, typeof(Thickness)));
+ throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(Thickness)}");
}
}
} \ No newline at end of file
diff --git a/Xamarin.Forms.Xaml.UnitTests/CompiledTypeConverter.xaml b/Xamarin.Forms.Xaml.UnitTests/CompiledTypeConverter.xaml
index b163064..296f9ee 100644
--- a/Xamarin.Forms.Xaml.UnitTests/CompiledTypeConverter.xaml
+++ b/Xamarin.Forms.Xaml.UnitTests/CompiledTypeConverter.xaml
@@ -7,5 +7,5 @@
BackgroundColor="Pink">
<Label x:Name="label"
HorizontalOptions="EndAndExpand"
- RelativeLayout.XConstraint="2" />
-</ContentPage> \ No newline at end of file
+ RelativeLayout.XConstraint="2" Margin="2,3"/>
+</ContentPage>
diff --git a/Xamarin.Forms.Xaml.UnitTests/CompiledTypeConverter.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/CompiledTypeConverter.xaml.cs
index 834a55c..1ef319a 100644
--- a/Xamarin.Forms.Xaml.UnitTests/CompiledTypeConverter.xaml.cs
+++ b/Xamarin.Forms.Xaml.UnitTests/CompiledTypeConverter.xaml.cs
@@ -38,7 +38,8 @@ namespace Xamarin.Forms.Xaml.UnitTests
Assert.AreEqual(LayoutOptions.EndAndExpand, p.label.GetValue(View.HorizontalOptionsProperty));
var xConstraint = RelativeLayout.GetXConstraint(p.label);
Assert.AreEqual(2, xConstraint.Compute(null));
+ Assert.AreEqual(new Thickness(2, 3), p.label.Margin);
}
}
}
-} \ No newline at end of file
+}