Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do page handler with MAUI

I am familiar with custom renderers from Xamarin Forms. I have been able to perform two custom view handler for MAUI. But, I am stuck with page handlers for MAUI... please help

What would be the equivalent of this pagerenderer from xamarin forms (iOS) for MAUI with handlers :

public class MyPageRenderer : PageRenderer, IUIGestureRecognizerDelegate
{
    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);

        if (NavigationController != null)
        {
            NavigationController.NavigationBarHidden = true;
            NavigationController.InteractivePopGestureRecognizer.Delegate  = this;
            NavigationController.InteractivePopGestureRecognizer.Enabled = true;
        }
    }
}

I try this but I don't understand how to map the ViewWillAppear method with a command mapper... It does not compile

public partial class MyPageHandler : PageHandler
    {
        public static MyCommandMapper<IContentView, IPageHandler> CommandMapper = new CommandMapper<IContentView, IPageHandler>(CommandMapper)
        {
        "ViewWillAppear" = MyViewWillAppear
        };

        protected override void ConnectHandler(ContentViewGroup platformView)
        {
            base.ConnectHandler(platformView);
        }

        protected override void DisconnectHandler(ContentViewGroup platformView)
        {
            base.DisconnectHandler(platformView);
        }

        public static void MyViewWillAppear(CustomEntryHandler handler, CustomEntry view)
        {
            if (NavigationController != null)
            {
                NavigationController.NavigationBarHidden = true;
                NavigationController.InteractivePopGestureRecognizer.Delegate  = this;
                NavigationController.InteractivePopGestureRecognizer.Enabled = true;
            }
        }
    }

I confirm I have added the handler in MauiProgram

like image 271
Maxime Esprit Avatar asked Sep 20 '25 04:09

Maxime Esprit


1 Answers

Thanks to the @Alexandar May - MSFT comment and following the https://github.com/dotnet/maui/issues/7174#issuecomment-1244019860 sample, here is the correct Handler :

#if IOS

    public class ContentPageHandler : Microsoft.Maui.Handlers.PageHandler
    {
        protected override void ConnectHandler(Microsoft.Maui.Platform.ContentView nativeView)
        {
            base.ConnectHandler(nativeView);
            ContentPage.Loaded += OnLoaded;
        }

        protected override void DisconnectHandler(Microsoft.Maui.Platform.ContentView nativeView)
        {
            ContentPage.Loaded -= OnLoaded;
            base.DisconnectHandler(nativeView);
        }

        ContentPage ContentPage => VirtualView as ContentPage;

        void OnLoaded(object sender, EventArgs e) => ConfigureInteractivePopGesture();

        void ConfigureInteractivePopGesture()
        {
            if (this is IPlatformViewHandler handler && handler.ViewController?.ParentViewController?.NavigationController is UIKit.UINavigationController navControler)
            {
                navControler.NavigationBarHidden = true;
                navControler.InteractivePopGestureRecognizer.Delegate = new GestureRecognizerDelegate();
                navControler.InteractivePopGestureRecognizer.Enabled = true;
            }
        }
    }

    public class GestureRecognizerDelegate : Foundation.NSObject, UIKit.IUIGestureRecognizerDelegate
    {

    }
#endif

And how to register it in MAUIProgram :

    public static MauiApp CreateMauiApp()
    {
        return MauiApp.CreateBuilder()
            .UseMauiApp<App>()
#if IOS
            .ConfigureMauiHandlers(handlers =>
            {
                handlers.AddHandler(typeof(ContentPage), typeof(ContentPageHandler));
            })
#endif
            .Build();
    }
like image 153
Maxime Esprit Avatar answered Sep 23 '25 07:09

Maxime Esprit