Skip to content

Fix for exception thrown when disconnecting page handlers in the Unloaded event while navigating on MacCatalyst and iOS#29410

Merged
PureWeen merged 8 commits intodotnet:inflight/currentfrom
KarthikRajaKalaimani:fix-29297
Sep 10, 2025
Merged

Fix for exception thrown when disconnecting page handlers in the Unloaded event while navigating on MacCatalyst and iOS#29410
PureWeen merged 8 commits intodotnet:inflight/currentfrom
KarthikRajaKalaimani:fix-29297

Conversation

@KarthikRajaKalaimani
Copy link
Copy Markdown
Contributor

@KarthikRajaKalaimani KarthikRajaKalaimani commented May 9, 2025

Issue Details:

When navigating away from the current page, the user disconnects all handlers including those of visual elements and the page itself during the Unloaded event of the current page. This leads to crash with a message " System.InvalidOperationException" is thrown when navigating back to the previous page.

Root Cause:

When navigating back to the previous page, the Unloaded event of the current page is triggered. As part of this event, the handler for the page is explicitly disconnected by the user. However, after the handler is cleared and the Unloaded event is attempts to save a token for wiring up the Loaded event via the SendLoaded method.

At this point, since the handler has already been set to null, calling SendLoaded results in a System.InvalidOperationException, as it requires a valid handler. This is why the exception only occurs during backward navigation.

Description of Change:

I have applied condition to call the SendLoaded method only if the handler is not null in iOS and Mac platform.

Tested the behavior in the following platforms.

  • Android
  • Windows
  • iOS
  • Mac

Reference:

N/A

Issues Fixed:

Fixes #29297

Screenshots

Before After
withoutfix.mov
withfix.mov

@dotnet-policy-service dotnet-policy-service bot added community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration labels May 9, 2025
@jsuarezruiz jsuarezruiz added platform/macos macOS / Mac Catalyst area-controls-shell Shell Navigation, Routes, Tabs, Flyout labels May 9, 2025
@@ -0,0 +1,27 @@
#if !ANDROID
//Skipping the test case to run in android because the loaded event is not triggered when navigating back to previous page.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you create a new bug and include the link to it here?

Copy link
Copy Markdown
Contributor Author

@KarthikRajaKalaimani KarthikRajaKalaimani May 9, 2025

Choose a reason for hiding this comment

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

I have logged new bug report for this issue.
#29414

@jsuarezruiz
Copy link
Copy Markdown
Contributor

/azp run MAUI-UITests-public

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@KarthikRajaKalaimani KarthikRajaKalaimani changed the title Fixed - Crash on shell navigation for MacCatalyst Fixed - Crash on shell navigation for MacCatalyst and iOS May 9, 2025
@KarthikRajaKalaimani KarthikRajaKalaimani changed the title Fixed - Crash on shell navigation for MacCatalyst and iOS Fix for exception thrown when disconnecting page handlers in the Unloaded event while navigating back to the previous page on MacCatalyst and iOS May 9, 2025
@KarthikRajaKalaimani KarthikRajaKalaimani changed the title Fix for exception thrown when disconnecting page handlers in the Unloaded event while navigating back to the previous page on MacCatalyst and iOS Fix for exception thrown when disconnecting page handlers in the Unloaded event while navigating on MacCatalyst and iOS May 9, 2025
@KarthikRajaKalaimani KarthikRajaKalaimani marked this pull request as ready for review May 9, 2025 13:56
@KarthikRajaKalaimani KarthikRajaKalaimani requested a review from a team as a code owner May 9, 2025 13:56
@rmarinho
Copy link
Copy Markdown
Member

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 3 pipeline(s).

_loadedUnloadedToken?.Dispose();
_loadedUnloadedToken = null;
_loadedUnloadedToken = this.OnLoaded(SendLoaded);
if (Handler is not null)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

@KarthikRajaKalaimani KarthikRajaKalaimani Jun 3, 2025

Choose a reason for hiding this comment

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

Can you also add the check for window? Window?.Handler?.PlatformView

Similar to https://github.com/dotnet/maui/pull/29410/files#diff-d5a3b4097b78c05a39e1aa317d8719e6010e0c17ca81bb81651cb902b5584d02R25

The user is disconnecting the page handler during the Unloaded event. If we use the code Windows?.Handler?.PlatformView the exception will still occurs. Therefore, we need to ensure that the page's handler is not null before proceeding.

@PureWeen PureWeen added this to the .NET 9 SR8 milestone Jun 2, 2025
@PureWeen PureWeen moved this from Todo to Changes Requested in MAUI SDK Ongoing Jun 2, 2025
@jsuarezruiz
Copy link
Copy Markdown
Contributor

/azp run MAUI-UITests-public

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@PureWeen PureWeen modified the milestones: .NET 9 SR8, .NET 9 SR9 Jun 9, 2025
@jsuarezruiz
Copy link
Copy Markdown
Contributor

/azp run MAUI-UITests-public

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@PureWeen PureWeen modified the milestones: .NET 9 SR9, .NET 9 SR10 Jul 3, 2025
@PureWeen
Copy link
Copy Markdown
Member

/rebase

@github-project-automation github-project-automation bot moved this from Changes Requested to Approved in MAUI SDK Ongoing Sep 10, 2025
@PureWeen PureWeen changed the base branch from main to inflight/current September 10, 2025 21:42
@PureWeen PureWeen merged commit 3c2ef75 into dotnet:inflight/current Sep 10, 2025
1 check passed
@github-project-automation github-project-automation bot moved this from Approved to Done in MAUI SDK Ongoing Sep 10, 2025
PureWeen pushed a commit that referenced this pull request Sep 18, 2025
…aded event while navigating on MacCatalyst and iOS (#29410)

* Fixed Crash on shell navigation for Mac Catalyst

* Test case added

* test case name modified

* updated the fix

* Issue linked

* curly brace added

* modified the condition.

* Modified the fix
PureWeen pushed a commit that referenced this pull request Sep 18, 2025
…aded event while navigating on MacCatalyst and iOS (#29410)

* Fixed Crash on shell navigation for Mac Catalyst

* Test case added

* test case name modified

* updated the fix

* Issue linked

* curly brace added

* modified the condition.

* Modified the fix
github-actions bot pushed a commit that referenced this pull request Sep 18, 2025
…aded event while navigating on MacCatalyst and iOS (#29410)

* Fixed Crash on shell navigation for Mac Catalyst

* Test case added

* test case name modified

* updated the fix

* Issue linked

* curly brace added

* modified the condition.

* Modified the fix
github-actions bot pushed a commit that referenced this pull request Sep 23, 2025
…aded event while navigating on MacCatalyst and iOS (#29410)

* Fixed Crash on shell navigation for Mac Catalyst

* Test case added

* test case name modified

* updated the fix

* Issue linked

* curly brace added

* modified the condition.

* Modified the fix
github-actions bot pushed a commit that referenced this pull request Sep 23, 2025
…aded event while navigating on MacCatalyst and iOS (#29410)

* Fixed Crash on shell navigation for Mac Catalyst

* Test case added

* test case name modified

* updated the fix

* Issue linked

* curly brace added

* modified the condition.

* Modified the fix
@github-actions github-actions bot locked and limited conversation to collaborators Oct 11, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-controls-shell Shell Navigation, Routes, Tabs, Flyout community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/macos macOS / Mac Catalyst

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Crash on shell navigation for Mac Catalyst

6 participants