Skip to content

NavigationPage.Navigation.RemovePage() fails to disconnect handlers when removing pages, unlike ContentPage.Navigation.RemovePage() #32239

@Vetle444

Description

@Vetle444

Description

When removing pages from a NavigationPage's navigation stack, using NavigationPage.Navigation.RemovePage() fails to properly disconnect handlers from the removed pages, while using ContentPage.Navigation.RemovePage() correctly disconnects handlers as expected.

Expected Behavior

When a page is removed from the navigation stack using RemovePage(), its handler should be disconnected regardless of whether you access the Navigation property through the NavigationPage or ContentPage.

Actual Behavior

  • Button_OnClicked2 (correct behavior): Uses ContentPage.Navigation.RemovePage() → handlers are properly disconnected from removed pages
  • Button_OnClicked (bug): Uses NavigationPage.Navigation.RemovePage() → handlers remain connected to removed pages, preventing proper cleanup

Code Example

// This FAILS to disconnect handlers (BUG)
private void Button_OnClicked(object? sender, EventArgs e)
{
    var navigationPage = Shell.Current.Navigation.ModalStack
        .FirstOrDefault(p => p is NavigationPage) as NavigationPage;
    var stack = navigationPage.Navigation.NavigationStack;
    var pages = stack.Take(stack.Count - 1).ToList();
    foreach (var page in pages)
    {
        navigationPage.Navigation.RemovePage(page); // Handlers stay connected
    }
    Navigation.PopModalAsync();
}

// This correctly disconnects handlers (Expected behavior)
private void Button_OnClicked2(object? sender, EventArgs e)
{
    var pages = Navigation.NavigationStack.Take(Navigation.NavigationStack.Count - 1).ToList();
    foreach (var page in pages)
    {
        Navigation.RemovePage(page); // Handlers are properly disconnected
    }
    Navigation.PopModalAsync();
}

Image

Steps to Reproduce

  1. Clone https://github.com/Vetle444/MauiTestApp/tree/popModalHandler
  2. Make sure to use branch popModalHandler
  3. Run app
  4. Click button Test Page Modal
  5. Click button Navigate to test page 2
  6. Click button Pop modal using NavigationPage
  7. Observe that no dialog appears describing that the root page was disconnected
  8. Now try popping modal using the other button
  9. Observe that the dialog appears

Link to public reproduction project repository

https://github.com/Vetle444/MauiTestApp/tree/popModalHandler

Version with bug

9.0.110 SR12

Is this a regression from previous behavior?

Yes, this used to work in Xamarin.Forms

Last version that worked well

No response

Affected platforms

iOS, Android, macOS, Windows

Affected platform versions

No response

Did you find any workaround?

Make sure to call .DisconnectHandlers after removing the page

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions