forked from dotnet/maui
-
Notifications
You must be signed in to change notification settings - Fork 0
Fix erratic Page.Loaded events in Shell tab navigation #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/fix-11
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
949fcda
Initial plan
Copilot 6a34b4d
Fix erratic Page.Loaded events in Shell tab navigation
Copilot 5364d9c
Add documentation and finalize Page.Loaded fix for Shell navigation
Copilot 64ade55
Add Android-specific fix for multiple Page.Loaded events
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
217 changes: 217 additions & 0 deletions
217
src/Controls/tests/TestCases.HostApp/Issues/ShellPageLoadedIssue.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using Microsoft.Maui.Controls; | ||
|
|
||
| namespace Maui.Controls.Sample.Issues | ||
| { | ||
| [Issue(IssueTracker.Github, 11, "For pages used in FlyoutItem/Tab/ShellContent the Page.Loaded event is called erratically", PlatformAffected.All)] | ||
| public class ShellPageLoadedIssue : Shell | ||
| { | ||
| public static Dictionary<string, int> PageLoadedCounts = new Dictionary<string, int>(); | ||
| public static Dictionary<string, List<string>> LoadedPageSequence = new Dictionary<string, List<string>>(); | ||
| public static string CurrentTestName = ""; | ||
|
|
||
| public ShellPageLoadedIssue() | ||
| { | ||
| var tab = new Tab { Title = "The Pages", Route = "ViewCaseViewModel" }; | ||
|
|
||
| tab.Items.Add(new ShellContent | ||
| { | ||
| Title = "Main Page", | ||
| ContentTemplate = new DataTemplate(() => new TestPage1()), | ||
| Route = "Route1" | ||
| }); | ||
|
|
||
| tab.Items.Add(new ShellContent | ||
| { | ||
| Title = "Other Page", | ||
| ContentTemplate = new DataTemplate(() => new TestPage2()), | ||
| Route = "Route2" | ||
| }); | ||
|
|
||
| tab.Items.Add(new ShellContent | ||
| { | ||
| Title = "Another Page", | ||
| ContentTemplate = new DataTemplate(() => new TestPage3()), | ||
| Route = "Route3" | ||
| }); | ||
|
|
||
| var flyoutItem = new FlyoutItem | ||
| { | ||
| FlyoutDisplayOptions = FlyoutDisplayOptions.AsMultipleItems | ||
| }; | ||
| flyoutItem.Items.Add(tab); | ||
|
|
||
| this.Items.Add(flyoutItem); | ||
|
|
||
| // Add single shell contents | ||
| this.Items.Add(new ShellContent | ||
| { | ||
| Title = "Single Main", | ||
| ContentTemplate = new DataTemplate(() => new TestPage1()), | ||
| Route = "MainPage" | ||
| }); | ||
|
|
||
| this.Items.Add(new ShellContent | ||
| { | ||
| Title = "Single Other", | ||
| ContentTemplate = new DataTemplate(() => new TestPage2()), | ||
| Route = "OtherPage" | ||
| }); | ||
| } | ||
|
|
||
| public static void ResetCounters(string testName) | ||
| { | ||
| CurrentTestName = testName; | ||
| PageLoadedCounts.Clear(); | ||
| if (!LoadedPageSequence.ContainsKey(testName)) | ||
| LoadedPageSequence[testName] = new List<string>(); | ||
| else | ||
| LoadedPageSequence[testName].Clear(); | ||
| } | ||
|
|
||
| public static void RecordPageLoaded(string pageName) | ||
| { | ||
| if (!PageLoadedCounts.ContainsKey(pageName)) | ||
| PageLoadedCounts[pageName] = 0; | ||
|
|
||
| PageLoadedCounts[pageName]++; | ||
|
|
||
| if (!string.IsNullOrEmpty(CurrentTestName)) | ||
| { | ||
| if (!LoadedPageSequence.ContainsKey(CurrentTestName)) | ||
| LoadedPageSequence[CurrentTestName] = new List<string>(); | ||
| LoadedPageSequence[CurrentTestName].Add($"{pageName}:{PageLoadedCounts[pageName]}"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public class TestPage1 : ContentPage | ||
| { | ||
| public TestPage1() | ||
| { | ||
| Title = "Main Page"; | ||
| AutomationId = "TestPage1"; | ||
| Content = new StackLayout | ||
| { | ||
| Children = | ||
| { | ||
| new Label { Text = "This is Test Page 1", AutomationId = "Page1Label" }, | ||
| new Button | ||
| { | ||
| Text = "Navigate to Other Page", | ||
| AutomationId = "NavigateToOtherButton", | ||
| Command = new Command(async () => await Shell.Current.GoToAsync("//Route2")) | ||
| }, | ||
| new Button | ||
| { | ||
| Text = "Navigate to Another Page", | ||
| AutomationId = "NavigateToAnotherButton", | ||
| Command = new Command(async () => await Shell.Current.GoToAsync("//Route3")) | ||
| }, | ||
| new Label { Text = GetLoadedCountText(), AutomationId = "LoadedCountLabel" } | ||
| } | ||
| }; | ||
|
|
||
| this.Loaded += (s, e) => | ||
| { | ||
| ShellPageLoadedIssue.RecordPageLoaded("TestPage1"); | ||
| if (Content is StackLayout stack && stack.Children.Last() is Label label) | ||
| { | ||
| label.Text = GetLoadedCountText(); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| private string GetLoadedCountText() | ||
| { | ||
| return $"Page1 Loaded Count: {(ShellPageLoadedIssue.PageLoadedCounts.ContainsKey("TestPage1") ? ShellPageLoadedIssue.PageLoadedCounts["TestPage1"] : 0)}"; | ||
| } | ||
| } | ||
|
|
||
| public class TestPage2 : ContentPage | ||
| { | ||
| public TestPage2() | ||
| { | ||
| Title = "Other Page"; | ||
| AutomationId = "TestPage2"; | ||
| Content = new StackLayout | ||
| { | ||
| Children = | ||
| { | ||
| new Label { Text = "This is Test Page 2", AutomationId = "Page2Label" }, | ||
| new Button | ||
| { | ||
| Text = "Navigate to Main Page", | ||
| AutomationId = "NavigateToMainButton", | ||
| Command = new Command(async () => await Shell.Current.GoToAsync("//Route1")) | ||
| }, | ||
| new Button | ||
| { | ||
| Text = "Navigate to Another Page", | ||
| AutomationId = "NavigateToAnotherButton", | ||
| Command = new Command(async () => await Shell.Current.GoToAsync("//Route3")) | ||
| }, | ||
| new Label { Text = GetLoadedCountText(), AutomationId = "LoadedCountLabel" } | ||
| } | ||
| }; | ||
|
|
||
| this.Loaded += (s, e) => | ||
| { | ||
| ShellPageLoadedIssue.RecordPageLoaded("TestPage2"); | ||
| if (Content is StackLayout stack && stack.Children.Last() is Label label) | ||
| { | ||
| label.Text = GetLoadedCountText(); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| private string GetLoadedCountText() | ||
| { | ||
| return $"Page2 Loaded Count: {(ShellPageLoadedIssue.PageLoadedCounts.ContainsKey("TestPage2") ? ShellPageLoadedIssue.PageLoadedCounts["TestPage2"] : 0)}"; | ||
| } | ||
| } | ||
|
|
||
| public class TestPage3 : ContentPage | ||
| { | ||
| public TestPage3() | ||
| { | ||
| Title = "Another Page"; | ||
| AutomationId = "TestPage3"; | ||
| Content = new StackLayout | ||
| { | ||
| Children = | ||
| { | ||
| new Label { Text = "This is Test Page 3", AutomationId = "Page3Label" }, | ||
| new Button | ||
| { | ||
| Text = "Navigate to Main Page", | ||
| AutomationId = "NavigateToMainButton", | ||
| Command = new Command(async () => await Shell.Current.GoToAsync("//Route1")) | ||
| }, | ||
| new Button | ||
| { | ||
| Text = "Navigate to Other Page", | ||
| AutomationId = "NavigateToOtherButton", | ||
| Command = new Command(async () => await Shell.Current.GoToAsync("//Route2")) | ||
| }, | ||
| new Label { Text = GetLoadedCountText(), AutomationId = "LoadedCountLabel" } | ||
| } | ||
| }; | ||
|
|
||
| this.Loaded += (s, e) => | ||
| { | ||
| ShellPageLoadedIssue.RecordPageLoaded("TestPage3"); | ||
| if (Content is StackLayout stack && stack.Children.Last() is Label label) | ||
| { | ||
| label.Text = GetLoadedCountText(); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| private string GetLoadedCountText() | ||
| { | ||
| return $"Page3 Loaded Count: {(ShellPageLoadedIssue.PageLoadedCounts.ContainsKey("TestPage3") ? ShellPageLoadedIssue.PageLoadedCounts["TestPage3"] : 0)}"; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Despite these changes, the Loaded event is still triggered multiple times , this issue occurs only on Android.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've implemented an Android-specific fix to address the multiple Loaded events issue. The problem was that Android's Fragment lifecycle in Shell navigation causes ViewAttachedToWindow/ViewDetachedFromWindow events to fire multiple times during tab switching, even with the initial ShellContent fix.
The solution adds platform-specific tracking in VisualElement to prevent redundant Loaded events by:
This should resolve the Android-specific multiple Loaded events while maintaining the cross-platform fix for Shell disconnect logic. Commit: 64ade55