From fc482ef2eb128dae25342613a3e6d6d0f676419a Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Thu, 2 Feb 2017 11:14:35 +0100 Subject: [PATCH] =?utf8?q?[Xaml[C]]=20throw=20meaningful=20exception=20whi?= =?utf8?q?le=20trying=20to=20set=20the=20content=20=E2=80=A6=20(#714)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * [Xaml[C]] throw meaningful exception while trying to set the content of a property without ContentPropertyAttribute * fix test --- Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs | 5 ++- Xamarin.Forms.Xaml.UnitTests/Issues/Bz43694.xaml | 21 ++++++++++ .../Issues/Bz43694.xaml.cs | 48 ++++++++++++++++++++++ Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs | 5 ++- 4 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Bz43694.xaml create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Bz43694.xaml.cs diff --git a/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs b/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs index 37dff29..235115d 100644 --- a/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs +++ b/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs @@ -128,7 +128,7 @@ namespace Xamarin.Forms.Build.Tasks // Collection element, implicit content, or implicit collection element. string contentProperty; var parentVar = Context.Variables[(IElementNode)parentNode]; - if (parentVar.VariableType.ImplementsInterface(Module.ImportReference(typeof (IEnumerable)))) + if (parentVar.VariableType.ImplementsInterface(Module.ImportReference(typeof (IEnumerable))) && parentVar.VariableType.GetMethods(md => md.Name == "Add" && md.Parameters.Count == 1, Module).Any()) { var elementType = parentVar.VariableType; if (elementType.FullName != "Xamarin.Forms.ResourceDictionary" && elementType.Resolve().BaseType.FullName != "Xamarin.Forms.ResourceDictionary") @@ -152,7 +152,8 @@ namespace Xamarin.Forms.Build.Tasks if (parentNode is IElementNode && ((IElementNode)parentNode).SkipProperties.Contains (propertyName)) return; Context.IL.Append(SetPropertyValue(Context.Variables[(IElementNode)parentNode], name, node, Context, node)); - } + } else + throw new XamlParseException($"Can not set the content of {((IElementNode)parentNode).XmlType.Name} as it doesn't have a ContentPropertyAttribute", node); } else if (IsCollectionItem(node, parentNode) && parentNode is ListNode) { diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz43694.xaml b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz43694.xaml new file mode 100644 index 0000000..9fcc0c0 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz43694.xaml @@ -0,0 +1,21 @@ + + + + + Dark + + + OpenSans-Light + OpenSans-Light + /Assets/Fonts/OpenSans-Light.ttf#Open Sans + + + + + \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz43694.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz43694.xaml.cs new file mode 100644 index 0000000..62af1c0 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz43694.xaml.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using NUnit.Framework; +using Xamarin.Forms; +using Xamarin.Forms.Core.UnitTests; + +namespace Xamarin.Forms.Xaml.UnitTests +{ + [XamlCompilation(XamlCompilationOptions.Skip)] + public partial class Bz43694 : ContentPage + { + public Bz43694() + { + InitializeComponent(); + } + + public Bz43694(bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + + [TestFixture] + class Tests + { + [SetUp] + public void Setup() + { + Device.PlatformServices = new MockPlatformServices(); + } + + [TearDown] + public void TearDown() + { + Device.PlatformServices = null; + } + + [TestCase(true)] + [TestCase(false)] + public void xStaticWithOnPlatformChildInRD(bool useCompiledXaml) + { + if (useCompiledXaml) + Assert.Throws(new XamlParseExceptionConstraint(9, 6), () => MockCompiler.Compile(typeof(Bz43694))); + else + Assert.Throws(new XamlParseExceptionConstraint(9, 6), () => new Bz43694(useCompiledXaml)); + } + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs b/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs index 47ef9f6..3ea1d1c 100644 --- a/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs +++ b/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs @@ -121,7 +121,7 @@ namespace Xamarin.Forms.Xaml } else if (IsCollectionItem(node, parentNode) && parentNode is IElementNode) { // Collection element, implicit content, or implicit collection element. string contentProperty; - if (typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(Context.Types [parentElement].GetTypeInfo())) { + if (typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(Context.Types [parentElement].GetTypeInfo()) && Context.Types[parentElement].GetRuntimeMethods().Any(mi => mi.Name == "Add" && mi.GetParameters().Length == 1)) { var source = Values [parentNode]; if (!(typeof(ResourceDictionary).IsAssignableFrom(Context.Types [parentElement]))) { var addMethod = @@ -137,7 +137,8 @@ namespace Xamarin.Forms.Xaml var source = Values [parentNode]; SetPropertyValue(source, name, value, Context.RootElement, node, Context, node); - } + } else + throw new XamlParseException($"Can not set the content of {((IElementNode)parentNode).XmlType.Name} as it doesn't have a ContentPropertyAttribute", node); } else if (IsCollectionItem(node, parentNode) && parentNode is ListNode) { var parentList = (ListNode)parentNode; var source = Values [parentNode.Parent]; -- 2.7.4