diff options
author | Stephane Delcroix <stephane@delcroix.org> | 2016-12-05 13:40:12 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-05 13:40:12 +0100 |
commit | f9a44ed5ba4db08d0855570a9ed6a618b6b80c56 (patch) | |
tree | d8a496ccfaf7682bba1bfb25d5024b0ec1a364a2 | |
parent | 1a5bead2f2e24cc16da23753eaf0882d38d54ea1 (diff) | |
download | xamarin-forms-f9a44ed5ba4db08d0855570a9ed6a618b6b80c56.tar.gz xamarin-forms-f9a44ed5ba4db08d0855570a9ed6a618b6b80c56.tar.bz2 xamarin-forms-f9a44ed5ba4db08d0855570a9ed6a618b6b80c56.zip |
[XamlC] Compile ContraintTypeConverters (#592)
* [XamlC] Compile ContraintTypeConverters
* fix error message
5 files changed, 45 insertions, 14 deletions
diff --git a/Xamarin.Forms.Build.Tasks/CompiledConverters/ConstraintTypeConverter.cs b/Xamarin.Forms.Build.Tasks/CompiledConverters/ConstraintTypeConverter.cs new file mode 100644 index 00000000..18b1810c --- /dev/null +++ b/Xamarin.Forms.Build.Tasks/CompiledConverters/ConstraintTypeConverter.cs @@ -0,0 +1,28 @@ +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 ConstraintTypeConverter : ICompiledTypeConverter + { + public IEnumerable<Instruction> ConvertFromString(string value, ModuleDefinition module, BaseNode node) + { + double size; + + if (string.IsNullOrEmpty(value) || !double.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out size)) + throw new XamlParseException($"Cannot convert \"{value}\" into {typeof(Constraint)}", node); + + yield return Instruction.Create(OpCodes.Ldc_R8, size); + + var constantDef = module.Import(typeof(Constraint)).Resolve().Methods.FirstOrDefault(md => md.IsStatic && md.Name == "Constant"); + var constantRef = module.Import(constantDef); + yield return Instruction.Create(OpCodes.Call, constantRef); + } + } +} diff --git a/Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj b/Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj index c845b597..9507ea3c 100644 --- a/Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj +++ b/Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj @@ -101,6 +101,7 @@ <Compile Include="Logger.cs" /> <Compile Include="XamlTask.cs" /> <Compile Include="CompiledMarkupExtensions\ArrayExtension.cs" /> + <Compile Include="CompiledConverters\ConstraintTypeConverter.cs" /> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Target Name="AfterBuild"> diff --git a/Xamarin.Forms.Core/ConstraintTypeConverter.cs b/Xamarin.Forms.Core/ConstraintTypeConverter.cs index fb0be513..04f3519b 100644 --- a/Xamarin.Forms.Core/ConstraintTypeConverter.cs +++ b/Xamarin.Forms.Core/ConstraintTypeConverter.cs @@ -3,6 +3,7 @@ using System.Globalization; namespace Xamarin.Forms { + [Xaml.ProvideCompiled("Xamarin.Forms.Core.XamlC.ConstraintTypeConverter")] public class ConstraintTypeConverter : TypeConverter { public override object ConvertFromInvariantString(string value) diff --git a/Xamarin.Forms.Xaml.UnitTests/CompiledTypeConverter.xaml b/Xamarin.Forms.Xaml.UnitTests/CompiledTypeConverter.xaml index 56d7e082..b1630646 100644 --- a/Xamarin.Forms.Xaml.UnitTests/CompiledTypeConverter.xaml +++ b/Xamarin.Forms.Xaml.UnitTests/CompiledTypeConverter.xaml @@ -1,9 +1,11 @@ -<?xml version="1.0" encoding="UTF-8"?> +<?xml version="1.0" encoding="UTF-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Xamarin.Forms.Xaml.UnitTests.CompiledTypeConverter" RectangleP="0,1,2,4" RectangleBP="4,8,16,32" BackgroundColor="Pink"> - <Label HorizontalOptions="EndAndExpand" /> + <Label x:Name="label" + HorizontalOptions="EndAndExpand" + RelativeLayout.XConstraint="2" /> </ContentPage>
\ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/CompiledTypeConverter.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/CompiledTypeConverter.xaml.cs index 092d67de..834a55c9 100644 --- a/Xamarin.Forms.Xaml.UnitTests/CompiledTypeConverter.xaml.cs +++ b/Xamarin.Forms.Xaml.UnitTests/CompiledTypeConverter.xaml.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; - -using Xamarin.Forms; -using NUnit.Framework; +using NUnit.Framework; namespace Xamarin.Forms.Xaml.UnitTests { @@ -31,14 +27,17 @@ namespace Xamarin.Forms.Xaml.UnitTests [TestFixture] public class Tests { - [TestCase (false)] - [TestCase (true)] - public void CompiledTypeConverterAreInvoked (bool useCompiledXaml) + [TestCase(false)] + [TestCase(true)] + public void CompiledTypeConverterAreInvoked(bool useCompiledXaml) { - var p = new CompiledTypeConverter (useCompiledXaml); - Assert.AreEqual (new Rectangle (0, 1, 2, 4), p.RectangleP); - Assert.AreEqual (new Rectangle (4, 8, 16, 32), p.RectangleBP); - Assert.AreEqual (Color.Pink, p.BackgroundColor); + var p = new CompiledTypeConverter(useCompiledXaml); + Assert.AreEqual(new Rectangle(0, 1, 2, 4), p.RectangleP); + Assert.AreEqual(new Rectangle(4, 8, 16, 32), p.RectangleBP); + Assert.AreEqual(Color.Pink, p.BackgroundColor); + Assert.AreEqual(LayoutOptions.EndAndExpand, p.label.GetValue(View.HorizontalOptionsProperty)); + var xConstraint = RelativeLayout.GetXConstraint(p.label); + Assert.AreEqual(2, xConstraint.Compute(null)); } } } |