Gesture Recognition does not work when Converting MAUI ContentView to Native UIViewController

86 Views Asked by At

I'm working with a .NET MAUI application and facing an issue when converting a ContentView to a native UIViewController.

My objective is to use native views while retaining all MAUI features, including gestures.

However, after the conversion, the gesture recognizers attached to the ContentView and children within the ContentView are not working in the resulting UIViewController.

The ContentView is not shown and will be shown only after the conversion.

Is there another way to fix this problem to ensure that gesture recognizers work as expected in the converted view?

 private static UIViewController ConvertToNativeView(this ContentView contentView)
    {
        var mauiContext = new MauiContext(RbtServiceProvider.Current);
    
        if (contentView.Handler is not ContentViewHandler handler)
        {
            handler = new ContentViewHandler();
            handler.SetMauiContext(mauiContext);
            contentView.Handler = handler;
            handler.SetVirtualView(contentView);
        }
    
        if (handler.MauiContext == null)
        {
            throw new InvalidOperationException($"Could not convert {nameof(contentView)} to native view");
        }
    
        var uiViewController = contentView.ToUIViewController(mauiContext);
        return uiViewController;
    }
0

There are 0 best solutions below