When using NavigationPage as the main page, I do not receive back button events.

Phunction 281 Reputation points
2025-01-21T18:05:54.9866667+00:00

Hi, in App.xaml.cscs I have my main page initiated like this:

MainPage = new NavigationPage(new MainPage());

However when I do that, this event is never called from any of my pages:

protected override bool OnBackButtonPressed()

If I use

 AppShell();

Then teh back button event works. However I am not able to use AppShell for other reasons.

How do I capture the OnBackButtonPressed() event?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,842 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 78,761 Reputation points Microsoft Vendor
    2025-01-22T02:48:39.7333333+00:00

    Hello,

    Based on your previous link, you implement OnBackButtonPressed in iOS on the latest Maui.

    I can reproduce this issue. Here is a solution, you can create a CustomNavigationPageRenderer in MauiAppproject/Platforms/iOS folder, then overrider PopViewController and get the current page, if the back button pressed, this PopViewController will be executed, get current page and invoke SendBackButtonPressed. If the value is true, do not execute back to the previous page. If false, you can back to the previous page.

    using Microsoft.Maui.Controls.Handlers.Compatibility;
    using UIKit;
     
    namespace MauiApp2.Platforms.iOS
    {
        public class CustomNavigationPageRenderer : NavigationRenderer
        {
            public override UIViewController PopViewController(bool animated)
            {
                if (Element is NavigationPage navigationPage)
                {
                    var currentPage = navigationPage.Navigation.NavigationStack.LastOrDefault();
                    if (currentPage != null)
                    {
                        // Invoke the back button pressed logic
                        var handled = currentPage.SendBackButtonPressed();
     
                        if (handled)
                        {
                            // If the back button press was handled, cancel the default behavior
                            return null;
                        }
                    }
                }
     
                // Proceed with the default back navigation
                return base.PopViewController(animated);
            }
        }
    }
    

    After that, you can reigster this CustomNavigationPageRenderer in the MauiProgram.cs for iOS platform.

    public static class MauiProgram
        {
            public static MauiApp CreateMauiApp()
            {
                var builder = MauiApp.CreateBuilder();
                builder
                    .UseMauiApp<App>()
                    .ConfigureFonts(fonts =>
                    {
                        fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                        fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                    })
                    .ConfigureMauiHandlers((handlers) =>
                    {
    #if  IOS
                        handlers.AddHandler(typeof(NavigationPage), typeof(MauiApp2.Platforms.iOS.CustomNavigationPageRenderer));
    #endif
                    });
    
    ...
    

    In the end, you can add OnBackButtonPressed in your detailed page.

      protected override bool OnBackButtonPressed()
       {
           //handle your operation
           return true;
       }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.