--- /dev/null
+using System.Collections.Generic;
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+using Mono.Cecil.Rocks;
+using Xamarin.Forms.Xaml;
+
+namespace Xamarin.Forms.Build.Tasks
+{
+ class ArrayExtension : ICompiledMarkupExtension
+ {
+ public IEnumerable<Instruction> ProvideValue(IElementNode node, ModuleDefinition module, ILContext context, out TypeReference memberRef)
+ {
+ var typeNode = node.Properties[new XmlName("", "Type")] as IElementNode;
+ var typeTypeRef = context.TypeExtensions[typeNode];
+ var n = node.CollectionItems.Count;
+
+ var instructions = new List<Instruction>();
+ instructions.Add(Instruction.Create(OpCodes.Ldc_I4, n));
+ instructions.Add(Instruction.Create(OpCodes.Newarr, typeTypeRef));
+
+ memberRef = typeTypeRef.MakeArrayType();
+ for (var i = 0; i < n; i++) {
+ instructions.Add(Instruction.Create(OpCodes.Dup));
+ instructions.Add(Instruction.Create(OpCodes.Ldc_I4, i));
+ instructions.Add(Instruction.Create(OpCodes.Ldloc, context.Variables[node.CollectionItems[i] as IElementNode]));
+ instructions.Add(Instruction.Create(OpCodes.Stelem_Ref));
+ }
+ return instructions;
+ }
+ }
+}
\ No newline at end of file
{
interface ICompiledMarkupExtension
{
- IEnumerable<Instruction> ProvideValue(IElementNode node, ModuleDefinition module, out TypeReference typeRef);
+ IEnumerable<Instruction> ProvideValue(IElementNode node, ModuleDefinition module, ILContext context, out TypeReference typeRef);
}
}
\ No newline at end of file
-using System;
-using System.Collections.Generic;
+using System.Collections.Generic;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Xamarin.Forms.Xaml;
{
class StaticExtension : ICompiledMarkupExtension
{
- public IEnumerable<Instruction> ProvideValue(IElementNode node, ModuleDefinition module, out TypeReference memberRef)
+ public IEnumerable<Instruction> ProvideValue(IElementNode node, ModuleDefinition module, ILContext context, out TypeReference memberRef)
{
INode ntype;
if (!node.Properties.TryGetValue(new XmlName("", "Member"), out ntype))
- ntype = node.CollectionItems [0];
+ ntype = node.CollectionItems[0];
var member = ((ValueNode)ntype).Value as string;
if (IsNullOrEmpty(member) || !member.Contains(".")) {
if (typeref.FullName == "Xamarin.Forms.Xaml.StaticExtension") {
var markupProvider = new StaticExtension();
- var il = markupProvider.ProvideValue(node, Module, out typeref);
+ var il = markupProvider.ProvideValue(node, Module, Context, out typeref);
var vardef = new VariableDefinition(typeref);
Context.Variables [node] = vardef;
Context.IL.Emit(OpCodes.Initobj, Module.Import(typedef));
}
- //if/when we land the compiled converters, those 2 blocks could be greatly simplified
if (typeref.FullName == "Xamarin.Forms.Xaml.TypeExtension") {
var visitor = new SetPropertiesVisitor(Context);
foreach (var cnode in node.Properties.Values.ToList())
Context.Body.Variables.Add(vardefref.VariableDefinition);
}
}
+
+ if (typeref.FullName == "Xamarin.Forms.Xaml.ArrayExtension")
+ {
+ var visitor = new SetPropertiesVisitor(Context);
+ foreach (var cnode in node.Properties.Values.ToList())
+ cnode.Accept(visitor, node);
+ foreach (var cnode in node.CollectionItems)
+ cnode.Accept(visitor, node);
+
+ var markupProvider = new ArrayExtension();
+
+ var il = markupProvider.ProvideValue(node, Module, Context, out typeref);
+
+ vardef = new VariableDefinition(typeref);
+ Context.Variables[node] = vardef;
+ Context.Body.Variables.Add(vardef);
+
+ Context.IL.Append(il);
+ Context.IL.Emit(OpCodes.Stloc, vardef);
+
+ //clean the node as it has been fully exhausted
+ node.Properties.Remove(new XmlName("","Type"));
+ node.CollectionItems.Clear();
+ }
}
}
static bool IsXaml2009LanguagePrimitive(IElementNode node)
{
- if (node.NamespaceURI == "http://schemas.microsoft.com/winfx/2009/xaml")
- return true;
+ if (node.NamespaceURI == "http://schemas.microsoft.com/winfx/2009/xaml") {
+ var n = node.XmlType.Name.Split(':') [1];
+ return n != "Array";
+ }
if (node.NamespaceURI != "clr-namespace:System;assembly=mscorlib")
return false;
var name = node.XmlType.Name.Split(':')[1];
genericArguments = null;
var typeDef = typeRef.Resolve();
TypeReference iface;
- if (
- (iface =
- typeDef.Interfaces.FirstOrDefault(
- tr =>
- tr.FullName.StartsWith(@interface) && tr.IsGenericInstance && (tr as GenericInstanceType).HasGenericArguments)) !=
- null)
+ if ((iface = typeDef.Interfaces.FirstOrDefault(tr =>
+ tr.FullName.StartsWith(@interface, StringComparison.Ordinal) &&
+ tr.IsGenericInstance && (tr as GenericInstanceType).HasGenericArguments)) != null)
{
interfaceReference = iface as GenericInstanceType;
genericArguments = (iface as GenericInstanceType).GenericArguments;
var arrayInterfaces = new[]
{
- "System.IEnumerable",
+ "System.Collections.IEnumerable",
"System.Collections.IList",
"System.Collections.Collection"
};
var arrayGenericInterfaces = new[]
{
- "System.IEnumerable`1",
+ "System.Collections.IEnumerable`1",
"System.Collections.Generic.IList`1",
"System.Collections.Generic.IReadOnlyCollection<T>",
"System.Collections.Generic.IReadOnlyList<T>",
"System.Collections.Generic.Collection<T>"
};
+ if (typeRef.IsArray && baseClass.IsArray) {
+ typeRef = typeRef.Resolve();
+ baseClass = baseClass.Resolve();
+ }
+
if (typeRef.IsArray)
{
var arrayType = typeRef.Resolve();
baseClass.IsGenericInstance &&
(baseClass as GenericInstanceType).GenericArguments[0].FullName == arrayType.FullName)
return true;
+ return false;
}
+
var typeDef = typeRef.Resolve();
if (typeDef.FullName == baseClass.FullName)
return true;
<Compile Include="CompiledConverters\RectangleTypeConverter.cs" />
<Compile Include="Logger.cs" />
<Compile Include="XamlTask.cs" />
+ <Compile Include="CompiledMarkupExtensions\ArrayExtension.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Target Name="AfterBuild">
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<local:MockView xmlns="http://xamarin.com/schemas/2014/forms"
+ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
+ xmlns:local="clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests"
+ x:Class="Xamarin.Forms.Xaml.UnitTests.FactoryMethodMissingCtor">
+ <local:MockView.Content>
+ <local:MockFactory>
+ <x:Arguments>
+ <x:Object/>
+ <x:String>bar</x:String>
+ <x:Int32>42</x:Int32>
+ </x:Arguments>
+ </local:MockFactory>
+ </local:MockView.Content>
+</local:MockView>
\ No newline at end of file
--- /dev/null
+using System;
+using NUnit.Framework;
+using Xamarin.Forms.Core.UnitTests;
+
+namespace Xamarin.Forms.Xaml.UnitTests
+{
+ [XamlCompilation(XamlCompilationOptions.Skip)]
+ public partial class FactoryMethodMissingCtor : MockView
+ {
+ public FactoryMethodMissingCtor()
+ {
+ InitializeComponent();
+ }
+
+ [TestFixture]
+ public class Tests
+ {
+ [SetUp]
+ public void SetUp()
+ {
+ Device.PlatformServices = new MockPlatformServices();
+ }
+
+ [TestCase(false)]
+ [TestCase(true)]
+ public void Throw(bool useCompiledXaml)
+ {
+ if (useCompiledXaml)
+ Assert.Throws(new XamlParseExceptionConstraint(7, 4), () => MockCompiler.Compile(typeof(FactoryMethodMissingCtor)));
+ else
+ Assert.Throws<MissingMethodException>(() => new FactoryMethodMissingCtor());
+ }
+ }
+ }
+}
\ No newline at end of file
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<local:MockView
+ xmlns="http://xamarin.com/schemas/2014/forms"
+ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
+ xmlns:local="clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests"
+ x:Class="Xamarin.Forms.Xaml.UnitTests.FactoryMethodMissingMethod">
+ <local:MockView.Content>
+ <local:MockFactory x:FactoryMethod="Factory">
+ <x:Arguments>
+ <x:Object/>
+ <x:String>bar</x:String>
+ <x:Int32>42</x:Int32>
+ </x:Arguments>
+ </local:MockFactory>
+ </local:MockView.Content>
+</local:MockView>
\ No newline at end of file
--- /dev/null
+using System;
+using NUnit.Framework;
+using Xamarin.Forms.Core.UnitTests;
+
+namespace Xamarin.Forms.Xaml.UnitTests
+{
+ [XamlCompilation(XamlCompilationOptions.Skip)]
+ public partial class FactoryMethodMissingMethod : MockView
+ {
+ public FactoryMethodMissingMethod()
+ {
+ InitializeComponent();
+ }
+
+ [TestFixture]
+ public class Tests
+ {
+ [SetUp]
+ public void SetUp()
+ {
+ Device.PlatformServices = new MockPlatformServices();
+ }
+
+ [TestCase(false)]
+ [TestCase(true)]
+ public void Throw(bool useCompiledXaml)
+ {
+ if (useCompiledXaml)
+ Assert.Throws(new XamlParseExceptionConstraint(8, 4), () => MockCompiler.Compile(typeof(FactoryMethodMissingMethod)));
+ else
+ Assert.Throws<MissingMemberException>(() => new FactoryMethodMissingMethod());
+ }
+ }
+ }
+}
+++ /dev/null
-using System;
-using NUnit.Framework;
-
-using Xamarin.Forms.Core.UnitTests;
-
-namespace Xamarin.Forms.Xaml.UnitTests
-{
- [TestFixture]
- public class FactoryMethodTests : BaseTestFixture
- {
- [Test]
- public void ThrowOnMissingCtor ()
- {
- var xaml = @"
- <local:MockView
- xmlns=""http://xamarin.com/schemas/2014/forms""
- xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""
- xmlns:local=""clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests"" >
- <local:MockView.Content>
- <local:MockFactory>
- <x:Arguments>
- <x:Object/>
- <x:String>bar</x:String>
- <x:Int32>42</x:Int32>
- </x:Arguments>
- </local:MockFactory>
- </local:MockView.Content>
- </local:MockView>";
- Assert.Throws<MissingMethodException> (()=>new MockView ().LoadFromXaml (xaml));
- }
-
- [Test]
- public void ThrowOnMissingMethod ()
- {
- var xaml = @"
- <local:MockView
- xmlns=""http://xamarin.com/schemas/2014/forms""
- xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""
- xmlns:local=""clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests"" >
- <local:MockView.Content>
- <local:MockFactory x:FactoryMethod=""Factory"">
- <x:Arguments>
- <x:Object/>
- <x:String>bar</x:String>
- <x:Int32>42</x:Int32>
- </x:Arguments>
- </local:MockFactory>
- </local:MockView.Content>
- </local:MockView>";
- Assert.Throws<MissingMemberException> (()=>new MockView ().LoadFromXaml (xaml));
- }
- }
-}
-<?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"
xmlns:local="clr-namespace:Xamarin.Forms.Xaml.UnitTests"
<local:MockView.Content>
<local:MockFactory x:FactoryMethod="ParameterlessFactory">
</local:MockFactory>
- </local:MockView.Content>
+ </local:MockView.Content>
</local:MockView>
<local:MockView x:Name="v4">
<local:MockView.Content>
<local:MockFactory x:Arguments="{x:Static local:MockxStatic.MockStaticProperty}"/>
</local:MockView.Content>
</local:MockView>
+ <local:MockView x:Name="v8">
+ <local:MockView.Content>
+ <local:MockFactory >
+ <x:Arguments>
+ <x:Array Type="{x:Type x:Object}">
+ <x:String>Foo</x:String>
+ <x:String>Bar</x:String>
+ </x:Array>
+ </x:Arguments>
+ </local:MockFactory>
+ </local:MockView.Content>
+ </local:MockView>
</StackLayout>
</ContentPage>
\ No newline at end of file
Content = "int ctor " + arg.ToString ();
}
+ public MockFactory(object [] args)
+ {
+ Content = string.Join(" ", args);
+ }
+
public static MockFactory ParameterlessFactory ()
{
return new MockFactory {
var layout = new FactoryMethods(useCompiledXaml);
Assert.AreEqual("alternate ctor Property", layout.v7.Content.Content);
}
+
+ [TestCase(false)]
+ [TestCase(true)]
+ public void TestCtorWithArrayParameter(bool useCompiledXaml)
+ {
+ var layout = new FactoryMethods(useCompiledXaml);
+ Assert.AreEqual("Foo Bar", layout.v8.Content.Content);
+ }
}
}
}
\ No newline at end of file
-<?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"
<Compile Include="OnPlatformTests.cs" />
<Compile Include="NullExtensionTests.cs" />
<Compile Include="TypeExtensionTests.cs" />
- <Compile Include="FactoryMethodTests.cs" />
<Compile Include="XamlgTests.cs" />
<Compile Include="Issues\TestCases.cs" />
<Compile Include="Issues\Issue1493.cs" />
<Compile Include="Issues\Bz47703.xaml.cs">
<DependentUpon>Bz47703.xaml</DependentUpon>
</Compile>
+ <Compile Include="FactoryMethodMissingCtor.xaml.cs">
+ <DependentUpon>FactoryMethodMissingCtor.xaml</DependentUpon>
+ </Compile>
+ <Compile Include="FactoryMethodMissingMethod.xaml.cs">
+ <DependentUpon>FactoryMethodMissingMethod.xaml</DependentUpon>
+ </Compile>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="..\.nuspec\Xamarin.Forms.Debug.targets" />
<EmbeddedResource Include="Issues\Bz47703.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
+ <EmbeddedResource Include="FactoryMethodMissingCtor.xaml">
+ <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
+ </EmbeddedResource>
+ <EmbeddedResource Include="FactoryMethodMissingMethod.xaml">
+ <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
+ </EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
Values[node] = value;
var markup = value as IMarkupExtension;
- if (markup != null && (value is TypeExtension || value is StaticExtension))
+ if (markup != null && (value is TypeExtension || value is StaticExtension || value is ArrayExtension))
{
var serviceProvider = new XamlServiceProvider(node, Context);
value = markup.ProvideValue(serviceProvider);
+ INode xKey;
+ if (!node.Properties.TryGetValue(XmlName.xKey, out xKey))
+ xKey = null;
+
node.Properties.Clear();
node.CollectionItems.Clear();
+ if (xKey != null)
+ node.Properties.Add(XmlName.xKey, xKey);
+
Values[node] = value;
}