[Xaml] Add provided value to collection (#4456)

This commit is contained in:
Akihiko Odaki 2018-11-23 18:00:46 +09:00 коммит произвёл Stephane Delcroix
Родитель 87dc917bea
Коммит e7bf945978
2 изменённых файлов: 48 добавлений и 1 удалений

Просмотреть файл

@ -0,0 +1,47 @@
using NUnit.Framework;
using System.Collections;
using Xamarin.Forms.Xaml;
namespace Xamarin.Forms.Xaml.UnitTests
{
[TestFixture]
public static class ApplyPropertiesVisitorTests
{
public class MarkupExtension : IMarkupExtension
{
public object ProvideValue(System.IServiceProvider serviceProvider)
{
return "provided value";
}
}
public class ArrayListOwner
{
public ArrayList ArrayList { get; } = new ArrayList();
}
[Test]
public static void ProvideValueForCollectionItem()
{
const string NAMESPACE = "clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests";
var resolver = new MockNameSpaceResolver();
var type = new XmlType(NAMESPACE, "ApplyPropertiesVisitorTests+MarkupExtension", null);
var listNode = new ListNode(new[]
{
new ElementNode(type, NAMESPACE, resolver),
new ElementNode(type, NAMESPACE, resolver)
}, resolver);
var rootElement = new ArrayListOwner();
var rootType = new XmlType(NAMESPACE, "ApplyPropertiesVisitorTests+ArrayListOwner", null);
var rootNode = new XamlLoader.RuntimeRootNode(rootType, rootElement, null);
var context = new HydrationContext { RootElement = rootElement };
rootNode.Properties.Add(new XmlName(null, "ArrayList"), listNode);
rootNode.Accept(new XamlNodeVisitor((node, parent) => node.Parent = parent), null);
rootNode.Accept(new CreateValuesVisitor(context), null);
rootNode.Accept(new ApplyPropertiesVisitor(context), null);
CollectionAssert.AreEqual(new[] { "provided value", "provided value" }, rootElement.ArrayList);
}
}
}

Просмотреть файл

@ -161,7 +161,7 @@ namespace Xamarin.Forms.Xaml
MethodInfo addMethod; MethodInfo addMethod;
if (xpe == null && (addMethod = collection.GetType().GetRuntimeMethods().First(mi => mi.Name == "Add" && mi.GetParameters().Length == 1)) != null) { if (xpe == null && (addMethod = collection.GetType().GetRuntimeMethods().First(mi => mi.Name == "Add" && mi.GetParameters().Length == 1)) != null) {
addMethod.Invoke(collection, new[] { Values[node] }); addMethod.Invoke(collection, new[] { value });
return; return;
} }
xpe = xpe ?? new XamlParseException($"Value of {parentList.XmlName.LocalName} does not have a Add() method", node); xpe = xpe ?? new XamlParseException($"Value of {parentList.XmlName.LocalName} does not have a Add() method", node);