1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
using System;
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 BoundsTypeConverter : ICompiledTypeConverter
{
IEnumerable<Instruction> ICompiledTypeConverter.ConvertFromString(string value, ModuleDefinition module, BaseNode node)
{
if (string.IsNullOrEmpty(value))
throw new XamlParseException($"Cannot convert \"{value}\" into {typeof(Rectangle)}", node);
double x = -1, y = -1, w = -1, h = -1;
bool hasX, hasY, hasW, hasH;
var xywh = value.Split(',');
if (xywh.Length != 2 && xywh.Length != 4)
throw new XamlParseException($"Cannot convert \"{value}\" into {typeof(Rectangle)}", node);
hasX = (xywh.Length == 2 || xywh.Length == 4) && double.TryParse(xywh [0], NumberStyles.Number, CultureInfo.InvariantCulture, out x);
hasY = (xywh.Length == 2 || xywh.Length == 4) && double.TryParse(xywh [1], NumberStyles.Number, CultureInfo.InvariantCulture, out y);
hasW = xywh.Length == 4 && double.TryParse(xywh [2], NumberStyles.Number, CultureInfo.InvariantCulture, out w);
hasH = xywh.Length == 4 && double.TryParse(xywh [3], NumberStyles.Number, CultureInfo.InvariantCulture, out h);
if (!hasW && xywh.Length == 4 && string.Compare("AutoSize", xywh [2].Trim(), StringComparison.OrdinalIgnoreCase) == 0) {
hasW = true;
w = AbsoluteLayout.AutoSize;
}
if (!hasH && xywh.Length == 4 && string.Compare("AutoSize", xywh [3].Trim(), StringComparison.OrdinalIgnoreCase) == 0) {
hasH = true;
h = AbsoluteLayout.AutoSize;
}
if (hasX && hasY && xywh.Length == 2) {
hasW = true;
w = AbsoluteLayout.AutoSize;
hasH = true;
h = AbsoluteLayout.AutoSize;
}
if (!hasX || !hasY || !hasW || !hasH)
throw new XamlParseException($"Cannot convert \"{value}\" into {typeof(Rectangle)}", node);
return GenerateIL(x, y, w, h, module);
}
IEnumerable<Instruction> GenerateIL(double x, double y, double w, double h, ModuleDefinition module)
{
// IL_0000: ldc.r8 3.1000000000000001
// IL_0009: ldc.r8 4.2000000000000002
// IL_0012: ldc.r8 5.2999999999999998
// IL_001b: ldc.r8 6.4000000000000004
// IL_0024: newobj instance void valuetype Test.Rectangle::'.ctor'(float64, float64, float64, float64)
yield return Instruction.Create(OpCodes.Ldc_R8, x);
yield return Instruction.Create(OpCodes.Ldc_R8, y);
yield return Instruction.Create(OpCodes.Ldc_R8, w);
yield return Instruction.Create(OpCodes.Ldc_R8, h);
var rectangleCtor = module.Import(typeof(Rectangle)).Resolve().Methods.FirstOrDefault(md => md.IsConstructor && md.Parameters.Count == 4);
var rectangleCtorRef = module.Import(rectangleCtor);
yield return Instruction.Create(OpCodes.Newobj, rectangleCtorRef);
}
}
}
|