summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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 00000000..e16a8b01
--- /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 9507ea3c..1ccace2b 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 5e79d25c..fa60267a 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 b1630646..296f9eeb 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 834a55c9..1ef319a3 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
+}