Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
[XamlG] creates fields for x:Name on root
Browse files Browse the repository at this point in the history
- fixes #2574
  • Loading branch information
StephaneDelcroix committed May 2, 2018
1 parent b118541 commit add5ec2
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 14 deletions.
16 changes: 6 additions & 10 deletions Xamarin.Forms.Build.Tasks/SetFieldVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,12 @@ public void Visit(ValueNode node, INode parentNode)
{
if (!IsXNameProperty(node, parentNode))
return;
if (!(parentNode is RootNode))
{
//no variable assigned for root
var field = Context.Body.Method.DeclaringType.Fields.SingleOrDefault(fd => fd.Name == (string)node.Value);
if (field == null)
return;
Context.IL.Emit(OpCodes.Ldarg_0);
Context.IL.Emit(OpCodes.Ldloc, Context.Variables[(IElementNode)parentNode]);
Context.IL.Emit(OpCodes.Stfld, field);
}
var field = Context.Body.Method.DeclaringType.Fields.SingleOrDefault(fd => fd.Name == (string)node.Value);
if (field == null)
return;
Context.IL.Emit(OpCodes.Ldarg_0);
Context.IL.Emit(OpCodes.Ldloc, Context.Variables[(IElementNode)parentNode]);
Context.IL.Emit(OpCodes.Stfld, field);
}

public void Visit(MarkupNode node, INode parentNode)
Expand Down
5 changes: 1 addition & 4 deletions Xamarin.Forms.Build.Tasks/XamlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,9 @@ static IEnumerable<CodeMemberField> GetCodeMemberFields(XmlNode root, XmlNamespa

XmlNodeList names =
root.SelectNodes(
"//*[@" + xPrefix + ":Name" +
"//*[@" + xPrefix + ":Name" +
"][not(ancestor:: __f__:DataTemplate) and not(ancestor:: __f__:ControlTemplate) and not(ancestor:: __f__:Style) and not(ancestor:: __f__:VisualStateManager.VisualStateGroups)]", nsmgr);
foreach (XmlNode node in names) {
// Don't take the root canvas
if (node == root)
continue;
var name = GetAttributeValue(node, "Name", XamlParser.X2006Uri, XamlParser.X2009Uri);
var typeArguments = GetAttributeValue(node, "TypeArguments", XamlParser.X2006Uri, XamlParser.X2009Uri);
var fieldModifier = GetAttributeValue(node, "FieldModifier", XamlParser.X2006Uri, XamlParser.X2009Uri);
Expand Down
9 changes: 9 additions & 0 deletions Xamarin.Forms.Xaml.UnitTests/Issues/Gh2574.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Xamarin.Forms.Xaml.UnitTests.Gh2574"
x:Name="page">
<ContentPage.Content>
</ContentPage.Content>
</ContentPage>
42 changes: 42 additions & 0 deletions Xamarin.Forms.Xaml.UnitTests/Issues/Gh2574.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using NUnit.Framework;
using Xamarin.Forms.Core.UnitTests;

namespace Xamarin.Forms.Xaml.UnitTests
{
public partial class Gh2574 : ContentPage
{
public Gh2574()
{
InitializeComponent();
}

public Gh2574(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(false),TestCase(true)]
public void xNameOnRoot(bool useCompiledXaml)
{
var layout = new Gh2574(useCompiledXaml);
Assert.That(layout.page, Is.EqualTo(layout));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,9 @@
<Compile Include="Issues\Gh2549.xaml.cs">
<DependentUpon>Gh2549.xaml</DependentUpon>
</Compile>
<Compile Include="Issues\Gh2574.xaml.cs">
<DependentUpon>Gh2574.xaml</DependentUpon>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

Expand Down Expand Up @@ -1109,6 +1112,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="Issues\Gh2574.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
Expand Down
19 changes: 19 additions & 0 deletions Xamarin.Forms.Xaml.UnitTests/XamlgTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,5 +308,24 @@ public void FieldModifier()
Assert.That(generator.NamedFields.First(cmf => cmf.Name == "publicLabel").Attributes, Is.EqualTo(MemberAttributes.Public));
}
}

[Test]
//https://github.com/xamarin/Xamarin.Forms/issues/2574
public void xNameOnRoot()
{
var xaml = @"<ContentPage
xmlns=""http://xamarin.com/schemas/2014/forms""
xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""
x:Class=""Foo""
x:Name=""bar"">
</ContentPage>";

var generator = new XamlGenerator();
generator.ParseXaml(new StringReader(xaml));

Assert.AreEqual(1, generator.NamedFields.Count());
Assert.AreEqual("bar", generator.NamedFields.First().Name);
Assert.AreEqual("Xamarin.Forms.ContentPage", generator.NamedFields.First().Type.BaseType);
}
}
}

0 comments on commit add5ec2

Please sign in to comment.