From 385dc4c6480cd490eb67428145a02ae897d25b1a Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Wed, 25 Jan 2017 15:09:54 +0100 Subject: [XamlC] allow xml-elements as Setter/Trigger Values (#684) --- .../CompiledValueProviders/SetterValueProvider.cs | 8 +++- .../CompiledValueProviders/TriggerValueProvider.cs | 8 +++- Xamarin.Forms.Xaml.UnitTests/Issues/Bz51567.xaml | 13 ++++++ .../Issues/Bz51567.xaml.cs | 47 ++++++++++++++++++++++ .../Xamarin.Forms.Xaml.UnitTests.csproj | 6 +++ 5 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Bz51567.xaml create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Bz51567.xaml.cs diff --git a/Xamarin.Forms.Build.Tasks/CompiledValueProviders/SetterValueProvider.cs b/Xamarin.Forms.Build.Tasks/CompiledValueProviders/SetterValueProvider.cs index 482ee7c6..60c3328f 100644 --- a/Xamarin.Forms.Build.Tasks/CompiledValueProviders/SetterValueProvider.cs +++ b/Xamarin.Forms.Build.Tasks/CompiledValueProviders/SetterValueProvider.cs @@ -5,6 +5,7 @@ using Mono.Cecil.Cil; using Xamarin.Forms.Xaml; using Xamarin.Forms.Build.Tasks; +using System.Xml; namespace Xamarin.Forms.Core.XamlC { @@ -12,7 +13,12 @@ namespace Xamarin.Forms.Core.XamlC { public IEnumerable ProvideValue(VariableDefinitionReference vardefref, ModuleDefinition module, BaseNode node, ILContext context) { - var valueNode = ((IElementNode)node).Properties[new XmlName("", "Value")]; + INode valueNode = null; + if (!((IElementNode)node).Properties.TryGetValue(new XmlName("", "Value"), out valueNode) && ((IElementNode)node).CollectionItems.Count == 1) + valueNode = ((IElementNode)node).CollectionItems[0]; + + if (valueNode == null) + throw new XamlParseException("Missing Value for Setter", (IXmlLineInfo)node); //if it's an elementNode, there's probably no need to convert it if (valueNode is IElementNode) diff --git a/Xamarin.Forms.Build.Tasks/CompiledValueProviders/TriggerValueProvider.cs b/Xamarin.Forms.Build.Tasks/CompiledValueProviders/TriggerValueProvider.cs index 6a0ca822..f355a528 100644 --- a/Xamarin.Forms.Build.Tasks/CompiledValueProviders/TriggerValueProvider.cs +++ b/Xamarin.Forms.Build.Tasks/CompiledValueProviders/TriggerValueProvider.cs @@ -5,6 +5,7 @@ using Mono.Cecil.Cil; using Xamarin.Forms.Xaml; using Xamarin.Forms.Build.Tasks; +using System.Xml; namespace Xamarin.Forms.Core.XamlC { @@ -12,7 +13,12 @@ namespace Xamarin.Forms.Core.XamlC { public IEnumerable ProvideValue(VariableDefinitionReference vardefref, ModuleDefinition module, BaseNode node, ILContext context) { - var valueNode = ((IElementNode)node).Properties[new XmlName("", "Value")]; + INode valueNode = null; + if (!((IElementNode)node).Properties.TryGetValue(new XmlName("", "Value"), out valueNode) && ((IElementNode)node).CollectionItems.Count == 1) + valueNode = ((IElementNode)node).CollectionItems[0]; + + if (valueNode == null) + throw new XamlParseException("Missing Value for Trigger", (IXmlLineInfo)node); //if it's an elementNode, there's probably no need to convert it if (valueNode is IElementNode) diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz51567.xaml b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz51567.xaml new file mode 100644 index 00000000..844d2d8c --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz51567.xaml @@ -0,0 +1,13 @@ + + + + + + + + \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz51567.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz51567.xaml.cs new file mode 100644 index 00000000..de64c2d1 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz51567.xaml.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using NUnit.Framework; +using Xamarin.Forms; +using Xamarin.Forms.Core.UnitTests; + +namespace Xamarin.Forms.Xaml.UnitTests +{ + public partial class Bz51567 : ContentPage + { + public Bz51567() + { + InitializeComponent(); + } + + public Bz51567(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 SetterWithElementValue(bool useCompiledXaml) + { + var page = new Bz51567(useCompiledXaml); + var style = page.Resources["ListText"] as Style; + var setter = style.Setters[1]; + Assert.NotNull(setter); + } + } + } +} diff --git a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj index b66a0824..608dfbde 100644 --- a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj +++ b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj @@ -406,6 +406,9 @@ Unreported008.xaml + + Bz51567.xaml + @@ -733,6 +736,9 @@ MSBuild:UpdateDesignTimeXaml + + MSBuild:UpdateDesignTimeXaml + -- cgit v1.2.3