Skip to content

Commit

Permalink
Add protected GetTemplateChild to ContentPage and ContentView xamarin…
Browse files Browse the repository at this point in the history
  • Loading branch information
andreinitescu authored and samhouts committed Jul 11, 2019
1 parent 4237219 commit ea132d0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
63 changes: 59 additions & 4 deletions Xamarin.Forms.Core.UnitTests/TemplatedViewUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void ShouldHaveTemplatedRootSet()
}

[Test]
public void GetTemplateChildShouldWork()
public void GetContentViewTemplateChildShouldWork()
{
var xaml = @"<ContentView
xmlns=""http://xamarin.com/schemas/2014/forms""
Expand All @@ -53,12 +53,32 @@ public void GetTemplateChildShouldWork()
contentView.LoadFromXaml(xaml);

IList<Element> internalChildren = contentView.InternalChildren;
var tc = (BindableObject)contentView.GetTemplateChild("label0");
Assert.AreEqual(tc, internalChildren[0]);
Assert.AreEqual(internalChildren[0], contentView.TemplateChildObtained);
}

[Test]
public void OnApplyTemplateShouldBeCalled()
public void GetContentPageTemplateChildShouldWork()
{
var xaml = @"<ContentPage
xmlns=""http://xamarin.com/schemas/2014/forms""
xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""
x:Class=""Xamarin.Forms.Core.UnitTests.MyTestContentPage"">
<ContentPage.ControlTemplate>
<ControlTemplate>
<Label x:Name=""label0""/>
</ControlTemplate>
</ContentPage.ControlTemplate>
</ContentPage>";

var contentPage = new MyTestContentPage();
contentPage.LoadFromXaml(xaml);

IList<Element> internalChildren = contentPage.InternalChildren;
Assert.AreEqual(internalChildren[0], contentPage.TemplateChildObtained);
}

[Test]
public void OnContentViewApplyTemplateShouldBeCalled()
{
var xaml = @"<ContentView
xmlns=""http://xamarin.com/schemas/2014/forms""
Expand All @@ -76,6 +96,25 @@ public void OnApplyTemplateShouldBeCalled()
Assert.IsTrue(contentView.WasOnApplyTemplateCalled);
}

[Test]
public void OnContentPageApplyTemplateShouldBeCalled()
{
var xaml = @"<ContentPage
xmlns=""http://xamarin.com/schemas/2014/forms""
xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""
x:Class=""Xamarin.Forms.Core.UnitTests.MyTestContentPage"">
<ContentPage.ControlTemplate>
<ControlTemplate>
<Label x:Name=""label0""/>
</ControlTemplate>
</ContentPage.ControlTemplate>
</ContentPage>";

var contentPage = new MyTestContentPage();
contentPage.LoadFromXaml(xaml);
Assert.IsTrue(contentPage.WasOnApplyTemplateCalled);
}

private class ExpectedView : View
{
public ExpectedView()
Expand Down Expand Up @@ -115,9 +154,25 @@ class MyTestContentView : ContentView
{
public bool WasOnApplyTemplateCalled { get; private set; }

public Element TemplateChildObtained { get; private set; }

protected override void OnApplyTemplate()
{
WasOnApplyTemplateCalled = true;
TemplateChildObtained = (Element)GetTemplateChild("label0");
}
}

class MyTestContentPage : ContentPage
{
public bool WasOnApplyTemplateCalled { get; private set; }

public Element TemplateChildObtained { get; private set; }

protected override void OnApplyTemplate()
{
WasOnApplyTemplateCalled = true;
TemplateChildObtained = (Element)GetTemplateChild("label0");
}
}
}
2 changes: 2 additions & 0 deletions Xamarin.Forms.Core/TemplatedPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,7 @@ protected override void OnChildRemoved(Element child)
base.OnChildRemoved(child);
TemplateUtilities.OnChildRemoved(this, child);
}

protected object GetTemplateChild(string name) => TemplateUtilities.GetTemplateChild(this, name);
}
}
2 changes: 2 additions & 0 deletions Xamarin.Forms.Core/TemplatedView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,7 @@ protected override void OnChildRemoved(Element child)
base.OnChildRemoved(child);
TemplateUtilities.OnChildRemoved(this, child);
}

protected object GetTemplateChild(string name) => TemplateUtilities.GetTemplateChild(this, name);
}
}

0 comments on commit ea132d0

Please sign in to comment.