summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Build.Tasks
diff options
context:
space:
mode:
authorStephane Delcroix <stephane@delcroix.org>2016-07-19 01:17:29 +0200
committerJason Smith <jason.smith@xamarin.com>2016-07-18 16:17:29 -0700
commitf304f25df2d80094d2c31fda4986f92454599a7e (patch)
treee9b9a32bea7e5f8c5e07fbcb4cabbaa5abb0c2f8 /Xamarin.Forms.Build.Tasks
parent272033723ea275ceb8a288fa605eafd035c79f2d (diff)
downloadxamarin-forms-f304f25df2d80094d2c31fda4986f92454599a7e.tar.gz
xamarin-forms-f304f25df2d80094d2c31fda4986f92454599a7e.tar.bz2
xamarin-forms-f304f25df2d80094d2c31fda4986f92454599a7e.zip
[Xaml] allow the Previewer to provide their own Xaml files for any type (#262)
* [Xaml] allow the Previewer to provide their own Xaml files for any type * [Xaml] use a Func instead of an interface, easier to use by reflection. Add tests * [XamlC] move the InitializeComponent duplication to XamlC task * [XamlC] generate branching code * [XamlC] fix the XamlC issue * [XamlC] make the API public * [docs] fix docs
Diffstat (limited to 'Xamarin.Forms.Build.Tasks')
-rw-r--r--Xamarin.Forms.Build.Tasks/DebugXamlCTask.cs15
-rw-r--r--Xamarin.Forms.Build.Tasks/XamlCTask.cs49
2 files changed, 53 insertions, 11 deletions
diff --git a/Xamarin.Forms.Build.Tasks/DebugXamlCTask.cs b/Xamarin.Forms.Build.Tasks/DebugXamlCTask.cs
index bec6386d..e0f81972 100644
--- a/Xamarin.Forms.Build.Tasks/DebugXamlCTask.cs
+++ b/Xamarin.Forms.Build.Tasks/DebugXamlCTask.cs
@@ -67,18 +67,11 @@ namespace Xamarin.Forms.Build.Tasks
LogLine(2, "no InitializeComponent found... skipped.");
continue;
}
- if (typeDef.Methods.FirstOrDefault(md => md.Name == "InitCompRuntime") != null)
- {
- LogLine(2, "InitCompRuntime already exists... skipped");
+ var initCompRuntime = typeDef.Methods.FirstOrDefault(md => md.Name == "__InitComponentRuntime");
+ if (initCompRuntime == null) {
+ LogLine(2, "no __InitComponentRuntime found... skipped.");
continue;
}
- LogLine(2, "");
-
- LogString(2, " Duplicating {0}.InitializeComponent () into {0}.InitCompRuntime ... ", typeDef.Name);
- var initCompRuntime = new MethodDefinition("InitCompRuntime", initComp.Attributes, initComp.ReturnType);
- initCompRuntime.Body = initComp.Body;
- typeDef.Methods.Add(initCompRuntime);
- LogLine(2, "done.");
// IL_0000: ldarg.0
// IL_0001: callvirt instance void class [Xamarin.Forms.Core]Xamarin.Forms.ContentPage::'.ctor'()
@@ -92,7 +85,7 @@ namespace Xamarin.Forms.Build.Tasks
// IL_0013: br IL_001e
//
// IL_0018: ldarg.0
- // IL_0019: callvirt instance void class Xamarin.Forms.Xaml.XamlcTests.MyPage::InitCompRuntime()
+ // IL_0019: callvirt instance void class Xamarin.Forms.Xaml.XamlcTests.MyPage::__InitComponentRuntime()
// IL_001e: ret
var altCtor =
diff --git a/Xamarin.Forms.Build.Tasks/XamlCTask.cs b/Xamarin.Forms.Build.Tasks/XamlCTask.cs
index 19548b80..b6387c0f 100644
--- a/Xamarin.Forms.Build.Tasks/XamlCTask.cs
+++ b/Xamarin.Forms.Build.Tasks/XamlCTask.cs
@@ -232,6 +232,16 @@ namespace Xamarin.Forms.Build.Tasks
}
LogLine(2, "");
+ if (typeDef.Methods.FirstOrDefault(md => md.Name == "__InitComponentRuntime") != null) {
+ LogLine(2, " __InitComponentRuntime already exists... not duplicating");
+ } else {
+ LogString(2, " Duplicating {0}.InitializeComponent () into {0}.__InitComponentRuntime ... ", typeDef.Name);
+ var initCompRuntime = new MethodDefinition("__InitComponentRuntime", initComp.Attributes, initComp.ReturnType);
+ initCompRuntime.Body = initComp.Body;
+ typeDef.Methods.Add(initCompRuntime);
+ LogLine(2, "done.");
+ }
+
LogString(2, " Parsing Xaml... ");
var rootnode = ParseXaml(resource.GetResourceStream(), typeDef);
if (rootnode == null)
@@ -249,6 +259,45 @@ namespace Xamarin.Forms.Build.Tasks
var body = new MethodBody(initComp);
var il = body.GetILProcessor();
il.Emit(OpCodes.Nop);
+
+ // Generating branching code for the Previewer
+ // IL_0007: call class [mscorlib]System.Func`2<class [mscorlib]System.Type,string> class [Xamarin.Forms.Xaml.Internals]Xamarin.Forms.Xaml.XamlLoader::get_XamlFileProvider()
+ // IL_000c: brfalse IL_0031
+ // IL_0011: call class [mscorlib]System.Func`2<class [mscorlib]System.Type,string> class [Xamarin.Forms.Xaml.Internals]Xamarin.Forms.Xaml.XamlLoader::get_XamlFileProvider()
+ // IL_0016: ldarg.0
+ // IL_0017: call instance class [mscorlib]System.Type object::GetType()
+ // IL_001c: callvirt instance !1 class [mscorlib]System.Func`2<class [mscorlib]System.Type, string>::Invoke(!0)
+ // IL_0021: brfalse IL_0031
+ // IL_0026: ldarg.0
+ // IL_0027: call instance void class Xamarin.Forms.Xaml.UnitTests.XamlLoaderGetXamlForTypeTests::__InitComponentRuntime()
+ // IL_002c: ret
+ // IL_0031: nop
+
+ var nop = Instruction.Create(OpCodes.Nop);
+ var getXamlFileProvider = body.Method.Module.Import(body.Method.Module.Import(typeof(Xamarin.Forms.Xaml.Internals.XamlLoader))
+ .Resolve()
+ .Properties.FirstOrDefault(pd => pd.Name == "XamlFileProvider")
+ .GetMethod);
+ il.Emit(OpCodes.Call, getXamlFileProvider);
+ il.Emit(OpCodes.Brfalse, nop);
+ il.Emit(OpCodes.Call, getXamlFileProvider);
+ il.Emit(OpCodes.Ldarg_0);
+ var getType = body.Method.Module.Import(body.Method.Module.Import(typeof(object))
+ .Resolve()
+ .Methods.FirstOrDefault(md => md.Name == "GetType"));
+ il.Emit(OpCodes.Call, getType);
+ var func = body.Method.Module.Import(body.Method.Module.Import(typeof(Func<Type, string>))
+ .Resolve()
+ .Methods.FirstOrDefault(md => md.Name == "Invoke"));
+ func = func.ResolveGenericParameters(body.Method.Module.Import(typeof(Func<Type, string>)), body.Method.Module);
+ il.Emit(OpCodes.Callvirt, func);
+ il.Emit(OpCodes.Brfalse, nop);
+ il.Emit(OpCodes.Ldarg_0);
+ var initCompRuntime = typeDef.Methods.FirstOrDefault(md => md.Name == "__InitComponentRuntime");
+ il.Emit(OpCodes.Call, initCompRuntime);
+ il.Emit(OpCodes.Ret);
+ il.Append(nop);
+
var visitorContext = new ILContext(il, body);
rootnode.Accept(new XamlNodeVisitor((node, parent) => node.Parent = parent), null);