From 1c796c3a9edfa6a5f16689e5e7ea775c8ef21339 Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Mon, 12 Dec 2016 10:14:37 +0100 Subject: [PATCH] [XamlC] support setting values on ValueTypes (#596) --- Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs | 16 ++++++-- Xamarin.Forms.Xaml.UnitTests/Issues/Bz46921.xaml | 47 ++++++++++++++++++++++ .../Issues/Bz46921.xaml.cs | 38 +++++++++++++++++ .../Xamarin.Forms.Xaml.UnitTests.csproj | 6 +++ 4 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Bz46921.xaml create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Bz46921.xaml.cs diff --git a/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs b/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs index 44678d2..35316f2 100644 --- a/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs +++ b/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs @@ -953,12 +953,19 @@ namespace Xamarin.Forms.Build.Tasks var valueNode = node as ValueNode; var elementNode = node as IElementNode; - yield return Instruction.Create(OpCodes.Ldloc, parent); + //if it's a value type, load the address so we can invoke methods on it + if (parent.VariableType.IsValueType) + yield return Instruction.Create(OpCodes.Ldloca, parent); + else + yield return Instruction.Create(OpCodes.Ldloc, parent); if (valueNode != null) { foreach (var instruction in valueNode.PushConvertedValue(context, propertyType, new ICustomAttributeProvider [] { property, propertyType.Resolve() }, valueNode.PushServiceProvider(context, propertyRef:property), false, true)) yield return instruction; - yield return Instruction.Create(OpCodes.Callvirt, propertySetterRef); + if (parent.VariableType.IsValueType) + yield return Instruction.Create(OpCodes.Call, propertySetterRef); + else + yield return Instruction.Create(OpCodes.Callvirt, propertySetterRef); } else if (elementNode != null) { var vardef = context.Variables [elementNode]; var implicitOperator = vardef.VariableType.GetImplicitOperatorTo(propertyType, module); @@ -970,7 +977,10 @@ namespace Xamarin.Forms.Build.Tasks yield return Instruction.Create(OpCodes.Unbox_Any, module.Import(propertyType)); else if (vardef.VariableType.IsValueType && propertyType.FullName == "System.Object") yield return Instruction.Create(OpCodes.Box, vardef.VariableType); - yield return Instruction.Create(OpCodes.Callvirt, propertySetterRef); + if (parent.VariableType.IsValueType) + yield return Instruction.Create(OpCodes.Call, propertySetterRef); + else + yield return Instruction.Create(OpCodes.Callvirt, propertySetterRef); } } diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz46921.xaml b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz46921.xaml new file mode 100644 index 0000000..ff32f20 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz46921.xaml @@ -0,0 +1,47 @@ + + + + + 4,20,4,20 + + + + + 4 + + + 20 + + + 20 + + + 4 + + + + + + 4 + + + 20 + + + 20 + + + 4 + + + + + diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz46921.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz46921.xaml.cs new file mode 100644 index 0000000..4f2f04d --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz46921.xaml.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using NUnit.Framework; +using Xamarin.Forms; + +namespace Xamarin.Forms.Xaml.UnitTests +{ + public partial class Bz46921 : ContentPage + { + public Bz46921() + { + InitializeComponent(); + } + + public Bz46921(bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + + [TestFixture] + class Tests + { + [TestCase(true)] + [TestCase(false)] + public void MultipleWaysToCreateAThicknessResource(bool useCompiledXaml) + { + var page = new Bz46921(useCompiledXaml); + foreach (var resname in new string[] { "thickness0", "thickness1", "thickness2", "thickness3", }) { + var resource = page.Resources[resname]; + Assert.That(resource, Is.TypeOf()); + var thickness = (Thickness)resource; + Assert.AreEqual(new Thickness(4, 20, 4, 20), thickness); + + } + } + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj index 4f56c57..5ce458c 100644 --- a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj +++ b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj @@ -397,6 +397,9 @@ FactoryMethodMissingMethod.xaml + + Bz46921.xaml + @@ -715,6 +718,9 @@ MSBuild:UpdateDesignTimeXaml + + MSBuild:UpdateDesignTimeXaml + -- 2.7.4