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 b0e3950
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
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(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 b0e3950

Please sign in to comment.