Skip to content

Commit 5b9bccd

Browse files
praveenkumarkarunanithiPureWeen
authored andcommitted
Fix for Handler not disconnected when removing a non-visible page from the navigation stack (#30049)
* Update NavigationProxy.cs * test case added * Update Issue29923.cs * fix updated
1 parent dd48b69 commit 5b9bccd

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed

src/Controls/src/Core/NavigationProxy.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ protected virtual void OnRemovePage(Page page)
244244
{
245245
currentInner.RemovePage(page);
246246
}
247+
page?.DisconnectHandlers();
247248
}
248249

249250
Page Pop()
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
namespace Maui.Controls.Sample.Issues;
2+
3+
[XamlCompilation(XamlCompilationOptions.Compile)]
4+
[Issue(IssueTracker.Github, 29923, "Removed page handlers not disconnected when using Navigation.RemovePage()", PlatformAffected.All)]
5+
public partial class Issue29923 : Shell
6+
{
7+
public Issue29923()
8+
{
9+
var rootPage = new ContentPage
10+
{
11+
Title = "Handler Disconnection Test",
12+
Content = new VerticalStackLayout
13+
{
14+
Padding = new Thickness(30, 0),
15+
Spacing = 25,
16+
Children =
17+
{
18+
new Button
19+
{
20+
Text = "Test Page Modal",
21+
AutomationId = "NavigateToTestPageModalButton",
22+
Command = new Command(async () =>
23+
{
24+
var navPage = new NavigationPage();
25+
var page1 = new Issue29923TestPage();
26+
var page2 = new Issue29923TestPage2();
27+
28+
await navPage.PushAsync(page1);
29+
await navPage.PushAsync(page2);
30+
31+
await Navigation.PushModalAsync(navPage);
32+
})
33+
},
34+
new Button
35+
{
36+
Text = "Test Page",
37+
AutomationId = "NavigateToTestPageButton",
38+
Command = new Command(async () =>
39+
{
40+
var page1 = new Issue29923TestPage();
41+
var page2 = new Issue29923TestPage2();
42+
43+
await Navigation.PushAsync(page1);
44+
await Navigation.PushAsync(page2);
45+
})
46+
}
47+
}
48+
}
49+
};
50+
51+
Items.Add(new ShellContent
52+
{
53+
Title = "Handler Test",
54+
Content = rootPage
55+
});
56+
}
57+
58+
public class Issue29923TestPage : ContentPage
59+
{
60+
public Issue29923TestPage()
61+
{
62+
Title = "Test Page";
63+
64+
Content = new VerticalStackLayout
65+
{
66+
Padding = new Thickness(20),
67+
Spacing = 10,
68+
};
69+
}
70+
}
71+
72+
public class Issue29923TestPage2 : ContentPage
73+
{
74+
private Label _handlerStatusLabel;
75+
76+
public Issue29923TestPage2()
77+
{
78+
Title = "Test Page 2";
79+
80+
_handlerStatusLabel = new Label
81+
{
82+
Text = "",
83+
AutomationId = "HandlerStatusLabel",
84+
FontAttributes = FontAttributes.Bold
85+
};
86+
87+
Content = new VerticalStackLayout
88+
{
89+
Padding = new Thickness(20),
90+
Spacing = 10,
91+
Children =
92+
{
93+
new Label
94+
{
95+
Text = "Test Page Removal Scenarios",
96+
FontSize = 18,
97+
FontAttributes = FontAttributes.Bold
98+
},
99+
new Button
100+
{
101+
Text = "Remove Test Page 1",
102+
AutomationId = "RemoveTestPage1Button",
103+
Command = new Command(RemoveFirstPage)
104+
},
105+
new Button
106+
{
107+
Text = "Pop Modal",
108+
AutomationId = "PopModalButton",
109+
Command = new Command(() => Navigation.PopModalAsync())
110+
},
111+
new HorizontalStackLayout
112+
{
113+
Children =
114+
{
115+
new Label { Text = "Is Handler still available:", FontAttributes = FontAttributes.Bold },
116+
_handlerStatusLabel
117+
}
118+
}
119+
}
120+
};
121+
}
122+
123+
private void RemoveFirstPage()
124+
{
125+
var navigationStack = Navigation.NavigationStack;
126+
if (navigationStack.Count == 0)
127+
return;
128+
129+
var pageToRemove = navigationStack.FirstOrDefault();
130+
if (pageToRemove == null && navigationStack.Count > 1)
131+
pageToRemove = navigationStack[1];
132+
133+
if (pageToRemove != null)
134+
{
135+
Navigation.RemovePage(pageToRemove);
136+
_handlerStatusLabel.Text = (pageToRemove.Handler != null).ToString();
137+
}
138+
}
139+
}
140+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue29923 : _IssuesUITest
8+
{
9+
public Issue29923(TestDevice testDevice) : base(testDevice)
10+
{
11+
}
12+
13+
public override string Issue => "Removed page handlers not disconnected when using Navigation.RemovePage()";
14+
15+
[Test]
16+
[Category(UITestCategories.Navigation)]
17+
public void RemovePageShouldDisconnectHandlers()
18+
{
19+
App.WaitForElement("NavigateToTestPageModalButton");
20+
App.Tap("NavigateToTestPageModalButton");
21+
App.WaitForElement("RemoveTestPage1Button");
22+
App.Tap("RemoveTestPage1Button");
23+
Assert.That(App.WaitForElement("HandlerStatusLabel").GetText(), Is.EqualTo("False"));
24+
App.Tap("PopModalButton");
25+
26+
App.WaitForElement("NavigateToTestPageButton");
27+
App.Tap("NavigateToTestPageButton");
28+
App.WaitForElement("RemoveTestPage1Button");
29+
App.Tap("RemoveTestPage1Button");
30+
Assert.That(App.WaitForElement("HandlerStatusLabel").GetText(), Is.EqualTo("False"));
31+
}
32+
}

0 commit comments

Comments
 (0)