Skip to content

How to override native control methods with maui handler pattern #2

@v-croft

Description

@v-croft

Hi @davidortinau.

I need to intercept the press and release events for controls in maui and this is a really helpful example.

I tried to implement the example within the context of our app but got a cast exception (the sample worked when run on its own).

As a better solution I tried to migrate the code to the maui handler pattern or directly within the control using HandlerChanged event. It is possible to make this work for Android as the Android.Views.View.Touch event is accessible via the PlatformView and for Windows via the PointerPressed and PointerReleased events. However, I cannot see how to override the UIView.TouchesBegan and UIView.TouchesCancelled methods for iOS using the handler pattern or directly (as you do with the Renderer).

public class PressableView : ContentView
{
    public PressableView ()
    {
        HandlerChanged += OnHandlerChanged;
    }

    void OnHandlerChanged(object sender, EventArgs e)
    {
        ContentView view = sender as ContentView;
#if ANDROID
        (view.Handler.PlatformView as Android.Views.View).Touch += Control_Touch;
#elif IOS || MACCATALYST
        // ?????
#elif WINDOWS
        (view.Handler.PlatformView as Microsoft.UI.Xaml.UIElement).PointerPressed += RaisePressed;
        (view.Handler.PlatformView as Microsoft.UI.Xaml.UIElement).PointerReleased += RaiseReleased;
#endif
    }


    //...
}

Am I missing something or is this a gap in the maui handler pattern?

I would really appreciate some help to point me in the right direction (I do not find any examples online) that address this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions