Search code examples
mauiskiasharp

Detect mouse wheel over SKCanvasView


I have a MAUI application for viewing images. Images are rendered using SKCanvasView. I wanted to add zoom functionality which, on windows, would work using mouse wheel. However I wasn't able to find a way to do it. The closes gesture to what I'm looking for seems to be PinchGesture, but mouse wheel doesn't get translated to it. I found that some controls have PointerWheelChanged event, but SKCanvasView is not one of them. I also heard of writing custom dependency services and handlers for this purpose, but no actual examples of such for mouse wheel.

I have a hunch that this is impossible in MAUI for now (unless I go really low level and tap into Windows APIs), but if someone knows a way how to make this work, I would appreciate it.


Solution

  • You can add the PointerWheelChanged event to the windows platform view of the SKCanvasView. Such as:

    In the xaml:

    <sk:SKCanvasView x:Name="skview" HeightRequest="300" WidthRequest="300"/>
    

    And in the page's code behind:

        protected override void OnHandlerChanged()
        {
            base.OnHandlerChanged();
    #if WINDOWS
                var view = skview.Handler.PlatformView as SkiaSharp.Views.Windows.SKXamlCanvas;
                view.PointerWheelChanged += ( s, e) =>
                {
    
                };
    #endif
          }
    

    I have tested this and it can detect the mouse wheel over SKCanvasView.