summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephane Delcroix <stephane@delcroix.org>2016-08-15 22:18:26 +0200
committerJason Smith <jason.smith@xamarin.com>2016-08-15 13:18:26 -0700
commita1126ab66bc9bd41cfa32dbefbe5758bcf5b4b32 (patch)
treed586aaffcf721ef9cd7558335757050a9c8b1f2e
parent90396b0ffdecf65b8e2eea8eb4f148bdea1071c4 (diff)
downloadxamarin-forms-a1126ab66bc9bd41cfa32dbefbe5758bcf5b4b32.tar.gz
xamarin-forms-a1126ab66bc9bd41cfa32dbefbe5758bcf5b4b32.tar.bz2
xamarin-forms-a1126ab66bc9bd41cfa32dbefbe5758bcf5b4b32.zip
[Xaml] Simplify listnodes with single elements (#304)
-rw-r--r--Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs14
-rw-r--r--Xamarin.Forms.Xaml.UnitTests/SetValue.xaml9
-rw-r--r--Xamarin.Forms.Xaml.UnitTests/SetValue.xaml.cs8
-rw-r--r--Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs18
-rw-r--r--Xamarin.Forms.Xaml/XmlName.cs1
5 files changed, 42 insertions, 8 deletions
diff --git a/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs b/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs
index b6bdf40b..5e11fe1d 100644
--- a/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs
+++ b/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs
@@ -79,6 +79,15 @@ namespace Xamarin.Forms.Build.Tasks
public void Visit(ElementNode node, INode parentNode)
{
+ XmlName propertyName = XmlName.Empty;
+
+ //Simplify ListNodes with single elements
+ var pList = parentNode as ListNode;
+ if (pList != null && pList.CollectionItems.Count == 1) {
+ propertyName = pList.XmlName;
+ parentNode = parentNode.Parent;
+ }
+
//if this node is an IMarkupExtension, invoke ProvideValue() and replace the variable
var vardef = Context.Variables[node];
var vardefref = new VariableDefinitionReference(vardef);
@@ -90,8 +99,7 @@ namespace Xamarin.Forms.Build.Tasks
Context.Variables[node] = vardef;
}
- XmlName propertyName;
- if (TryGetPropertyName(node, parentNode, out propertyName))
+ if (propertyName != XmlName.Empty || TryGetPropertyName(node, parentNode, out propertyName))
{
if (skips.Contains(propertyName))
return;
@@ -675,4 +683,4 @@ namespace Xamarin.Forms.Build.Tasks
return vardefref.VariableDefinition;
}
}
-} \ No newline at end of file
+}
diff --git a/Xamarin.Forms.Xaml.UnitTests/SetValue.xaml b/Xamarin.Forms.Xaml.UnitTests/SetValue.xaml
index 133e0e9b..da844ed0 100644
--- a/Xamarin.Forms.Xaml.UnitTests/SetValue.xaml
+++ b/Xamarin.Forms.Xaml.UnitTests/SetValue.xaml
@@ -3,6 +3,9 @@
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Xamarin.Forms.Xaml.UnitTests"
+ xmlns:d="ignoreme"
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+ mc:Ignorable="d"
x:Class="Xamarin.Forms.Xaml.UnitTests.SetValue">
<ContentPage.Resources>
<ResourceDictionary>
@@ -74,5 +77,11 @@
<StackLayout x:Name="stack4">
<local:ConvertibleToView/>
</StackLayout>
+ <ContentView x:Name="contentview2">
+ <ContentView.Content>
+ <Label />
+ <d:IgnorableElement />
+ </ContentView.Content>
+ </ContentView>
</StackLayout>
</ContentPage> \ No newline at end of file
diff --git a/Xamarin.Forms.Xaml.UnitTests/SetValue.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/SetValue.xaml.cs
index ce377a71..2226402c 100644
--- a/Xamarin.Forms.Xaml.UnitTests/SetValue.xaml.cs
+++ b/Xamarin.Forms.Xaml.UnitTests/SetValue.xaml.cs
@@ -207,6 +207,14 @@ namespace Xamarin.Forms.Xaml.UnitTests
var page = new SetValue(useCompiledXaml);
Assert.That(page.stack4.Children[0], Is.TypeOf<Button>());
}
+
+ [TestCase(false)]
+ [TestCase(true)]
+ public void ListsAreSimplified(bool useCompiledXaml)
+ {
+ var page = new SetValue(useCompiledXaml);
+ Assert.That(page.contentview2.Content, Is.TypeOf<Label>());
+ }
}
}
} \ No newline at end of file
diff --git a/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs b/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs
index 1e332323..108d707e 100644
--- a/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs
+++ b/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs
@@ -102,21 +102,29 @@ namespace Xamarin.Forms.Xaml
value = valueProvider.ProvideValue(serviceProvider);
}
- XmlName propertyName;
- if (TryGetPropertyName(node, parentNode, out propertyName))
- {
+ XmlName propertyName = XmlName.Empty;
+
+ //Simplify ListNodes with single elements
+ var pList = parentNode as ListNode;
+ if (pList != null && pList.CollectionItems.Count == 1) {
+ propertyName = pList.XmlName;
+ parentNode = parentNode.Parent;
+ parentElement = parentNode as IElementNode;
+ }
+
+ if (propertyName != XmlName.Empty || TryGetPropertyName(node, parentNode, out propertyName)) {
if (Skips.Contains(propertyName))
return;
if (parentElement.SkipProperties.Contains(propertyName))
return;
- var source = Values[parentNode];
+ var source = Values [parentNode];
if (propertyName == XmlName._CreateContent && source is ElementTemplate)
SetTemplate(source as ElementTemplate, node);
else
SetPropertyValue(source, propertyName, value, Context.RootElement, node, Context, node);
- }
+ }
else if (IsCollectionItem(node, parentNode) && parentNode is IElementNode)
{
// Collection element, implicit content, or implicit collection element.
diff --git a/Xamarin.Forms.Xaml/XmlName.cs b/Xamarin.Forms.Xaml/XmlName.cs
index e22a162d..09cf3bca 100644
--- a/Xamarin.Forms.Xaml/XmlName.cs
+++ b/Xamarin.Forms.Xaml/XmlName.cs
@@ -11,6 +11,7 @@ namespace Xamarin.Forms.Xaml
public static readonly XmlName xTypeArguments = new XmlName("x", "TypeArguments");
public static readonly XmlName xArguments = new XmlName("x", "Arguments");
public static readonly XmlName xFactoryMethod = new XmlName("x", "xFactoryMethod");
+ public static readonly XmlName Empty = new XmlName();
public string NamespaceURI { get; }