Skip to content

Commit bec3377

Browse files
authored
Fixed content update issue when changing the ControlTemplate at runtime for a ContentPage (#28216)
* Fixed the ControlTemplate runtime issue on ContentPage * Update Issue15649.xaml Changes added.
1 parent 0f1060c commit bec3377

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

src/Controls/src/Core/TemplatedPage.cs

+5
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ void IControlTemplated.OnApplyTemplate()
5959

6060
protected virtual void OnApplyTemplate()
6161
{
62+
OnApplyTemplateImpl();
63+
}
64+
void OnApplyTemplateImpl()
65+
{
66+
Handler?.UpdateValue(nameof(IContentView.Content));
6267
}
6368

6469
protected override void OnChildRemoved(Element child, int oldLogicalIndex)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
x:Class="Controls.TestCases.HostApp.Issues.Issue15649"
5+
xmlns:local="clr-namespace:Controls.TestCases.HostApp.Issues"
6+
x:Name="HomePage"
7+
Title="Issue15649">
8+
<ContentPage.Resources>
9+
<ResourceDictionary>
10+
<ControlTemplate x:Key="Page1">
11+
<StackLayout BackgroundColor="Red">
12+
<Button Text="Change Value"
13+
AutomationId="Button"
14+
Clicked="Button_Clicked"/>
15+
<Label Text="First Page"
16+
AutomationId="Page1Label"
17+
HorizontalOptions="Center"
18+
VerticalOptions="Center"></Label>
19+
20+
</StackLayout>
21+
</ControlTemplate>
22+
<ControlTemplate x:Key="Page2">
23+
<StackLayout BackgroundColor="Blue">
24+
<Label Text="Second Page"
25+
AutomationId="Page2Label"
26+
HorizontalOptions="Center"
27+
VerticalOptions="Center"></Label>
28+
</StackLayout>
29+
30+
</ControlTemplate>
31+
</ResourceDictionary>
32+
33+
</ContentPage.Resources>
34+
<ContentPage.Triggers>
35+
<DataTrigger TargetType="ContentPage"
36+
Binding="{Binding PositionSelected, Source={x:Reference HomePage},x:DataType=local:Issue15649}"
37+
Value="1">
38+
<Setter Property="ControlTemplate"
39+
Value="{DynamicResource Page1}"/>
40+
</DataTrigger>
41+
<DataTrigger TargetType="ContentPage"
42+
Binding="{Binding PositionSelected, Source={x:Reference HomePage},x:DataType=local:Issue15649}"
43+
Value="2">
44+
<Setter Property="ControlTemplate"
45+
Value="{DynamicResource Page2}"/>
46+
</DataTrigger>
47+
</ContentPage.Triggers>
48+
</ContentPage>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace Controls.TestCases.HostApp.Issues;
2+
3+
[Issue(IssueTracker.Github, 15649, "Updating a ControlTemplate at runtime for a Content Page is not working.", PlatformAffected.All)]
4+
public partial class Issue15649 : ContentPage
5+
{
6+
public int _positionSelected = 1;
7+
8+
public int PositionSelected
9+
{
10+
set
11+
{
12+
if (_positionSelected != value)
13+
{
14+
_positionSelected = value;
15+
16+
OnPropertyChanged();
17+
}
18+
}
19+
get => _positionSelected;
20+
}
21+
22+
public Issue15649()
23+
{
24+
InitializeComponent();
25+
this.BindingContext = this;
26+
}
27+
private void Button_Clicked(object sender, EventArgs e)
28+
{
29+
PositionSelected = 2;
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue15649(TestDevice testDevice) : _IssuesUITest(testDevice)
8+
{
9+
public override string Issue => "Updating a ControlTemplate at runtime for a Content Page is not working.";
10+
11+
[Test]
12+
[Category(UITestCategories.Page)]
13+
public void DynamicallyUpdatingContentPage()
14+
{
15+
App.WaitForElement("Page1Label");
16+
App.Tap("Button");
17+
App.WaitForElement("Page2Label");
18+
}
19+
}

0 commit comments

Comments
 (0)