Skip to content

Added OnNavigatingFrom extension for popup related navigation #3099

Draft
dartasen wants to merge 13 commits intoCommunityToolkit:mainfrom
dartasen:feat/addnavigatingextension
Draft

Added OnNavigatingFrom extension for popup related navigation #3099
dartasen wants to merge 13 commits intoCommunityToolkit:mainfrom
dartasen:feat/addnavigatingextension

Conversation

@dartasen
Copy link
Copy Markdown

@dartasen dartasen commented Feb 16, 2026

Description of Change

Added an extension method to OnNavigatingFrom event which checks if the destination page is a popup and if so returns true.

Currently one of the test is failing because MAUI OnNavigatingFromEventArgs seems to provide a wrong DestinationPage argument. Rather than acting like OnNavigatedFrom, it gives the CurrentPage rather than the real navigation destination.

I opened an issue : dotnet/maui#34073

Linked Issues

PR Checklist

Additional information

Copilot AI review requested due to automatic review settings February 16, 2026 11:05
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds an extension method for the OnNavigatingFrom navigation event to detect when navigation is transitioning to a Community Toolkit Popup. The implementation mirrors the existing NavigatedFromEventArgsExtensions but operates during the "navigating" phase rather than the "navigated" phase, allowing developers to detect popup navigation before it completes.

Changes:

  • Added NavigatingFromEventArgsExtensions.shared.cs with IsDestinationPageACommunityToolkitPopupPage() extension method
  • Updated documentation in NavigatedFromEventArgsExtensions.shared.cs for consistency
  • Added comprehensive unit tests in NavigatingFromEventArgsExtensionsTests.cs with positive and negative test cases

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
NavigatingFromEventArgsExtensions.shared.cs New extension class providing IsDestinationPageACommunityToolkitPopupPage() method to detect popup navigation during the navigating phase
NavigatedFromEventArgsExtensions.shared.cs Updated documentation comments to use "if" instead of "whether" for consistency with the new extension method
NavigatingFromEventArgsExtensionsTests.cs Added unit tests covering both true and false cases for popup detection during navigation

Copy link
Copy Markdown
Collaborator

@TheCodeTraveler TheCodeTraveler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dartasen!! Approved ✅

Could you please also open a Docs PR on our official documentation repo? https://github.com/MicrosoftDocs/CommunityToolkit/pulls

Once the Docs PR is submitted, I can merge the code here. I plan on releasing CommunityToolkit.Maui v14.0.1 this week once Microsoft.Maui.Controls v10.0.41 is released. If you're able to get the Docs PR in before Friday, we'll include this in the scheduled release and publish it as CommunityToolkit.Maui v14.1.0 instead!

@TheCodeTraveler TheCodeTraveler added the approved This Proposal has been approved and is ready to be added to the Toolkit label Feb 17, 2026
@dartasen
Copy link
Copy Markdown
Author

Thank's @TheCodeTraveler, will look at adding the documentation tomorrow !

However what's the approach you wanna take here ?
Currently the UnitTests are failing until OnNavigatingFrom get fixes in MAUI.
Should I remove them and file an issue to add them back as soon as it's fixed ?

@dartasen
Copy link
Copy Markdown
Author

Added the documentation 😄

@TheCodeTraveler
Copy link
Copy Markdown
Collaborator

TheCodeTraveler commented Feb 18, 2026

Currently the UnitTests are failing until dotnet/maui#34073 get fixes in MAUI

Oh wow - strange! Yea, you're totally right. This is a bug in .NET MAUI: NavigatingFromEventArgs.DestinationPage is actually providing the previous page. Testing it alongside NavigatedFromEventArgs.DestinationPage, we confirm that NavigatingFromEventArgs.DestinationPage provides the destination page.

I confirmed that the unit test is accurate. I also implemented OnNavigatingFrom in the Sample app to confirm it wasn't just a bad test.

Screenshots of Bug

Here's two screenshots showing a breakpoint triggering when I display SimplePopup from our Popups Page in the sample app:

NavigatingFromEventArgs.DestinationPage Incorrect NavigatedFromEventArgs.DestinationPage Incorrect

And here's a GIF demonstrating that we are correctly navigating to a PopupPage but NavigatingFromEventArgs is proving the incorrect value its DestinationPage property.

ScreenFlow

Next Steps

In the meantime, I'll convert this PR to a draft and keep an eye on the Issue you opened. Once that Issue is fixed, we'll merge this PR 🙌

@dartasen
Copy link
Copy Markdown
Author

Fixed in dotnet/maui#34404
Should be part of the next SR 😄

@TheCodeTraveler TheCodeTraveler requested a review from Copilot March 25, 2026 21:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 6 comments.

Comment on lines +39 to +43
if (sender is not ShellContentPage)
{
shellContentPage.NavigatingFromEventArgsReceived -= HandleNavigatingFromEventArgsReceived;
isDestinationPageACommunityToolkitPopupPageTCS.SetResult(e.IsDestinationPageACommunityToolkitPopupPage());
}
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HandleNavigatingFromEventArgsReceived never sets the TaskCompletionSource because sender is always ShellContentPage (the event is invoked with this in ShellContentPage.OnNavigatingFrom). As written, the test can hang until timeout. Remove/fix the if (sender is not ShellContentPage) condition and unsubscribe + set the result on the expected navigating-from invocation.

Suggested change
if (sender is not ShellContentPage)
{
shellContentPage.NavigatingFromEventArgsReceived -= HandleNavigatingFromEventArgsReceived;
isDestinationPageACommunityToolkitPopupPageTCS.SetResult(e.IsDestinationPageACommunityToolkitPopupPage());
}
shellContentPage.NavigatingFromEventArgsReceived -= HandleNavigatingFromEventArgsReceived;
isDestinationPageACommunityToolkitPopupPageTCS.SetResult(e.IsDestinationPageACommunityToolkitPopupPage());

Copilot uses AI. Check for mistakes.
TheCodeTraveler and others added 3 commits March 25, 2026 14:47
…ventArgsExtensionsTests.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…ions.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…ions.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@TheCodeTraveler
Copy link
Copy Markdown
Collaborator

TheCodeTraveler commented Mar 25, 2026

Dang. Still blocked on Microsoft.Maui.Controls v10.0.51.

@dartasen
Copy link
Copy Markdown
Author

@TheCodeTraveler 5.1 is "just" a cold fix, it should hopefully be part of SR6 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved This Proposal has been approved and is ready to be added to the Toolkit blocked

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants