Mac cursor on button

Dani_S 3,336 Reputation points
2024-05-30T10:34:02.6266667+00:00

Hi,

In window I did this code and is not working on MAC.

How do i fix it ?

Thanks,

        Microsoft.Maui.Handlers.ButtonHandler.Mapper.AppendToMapping("Custom", (handler, view) =>

        {

#if WINDOWS

using Microsoft.UI.Input;

using Microsoft.UI.Xaml;

#endif

using System.Reflection;

namespace GssdDesktopClient.Maui.Extensions

{

public static class ElementExtension

{

#if WINDOWS

    public static void ChangeCursor(this UIElement uiElement, InputCursor cursor)

    {

        Type type = typeof(UIElement);

        type.InvokeMember("ProtectedCursor", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty | BindingFlags.Instance, null, uiElement, new object[] { cursor });

    }

#endif

}

}

#if WINDOWS

        Button button = handler.VirtualView as Button;

        button.HandlerChanged    += (sender, args) =>

        {

            Microsoft.UI.Xaml.Controls.Button btn = (Microsoft.UI.Xaml.Controls.Button)handler.PlatformView;

            btn.Loaded += (s2, e2) =>

            {

                MainThread.BeginInvokeOnMainThread(() =>

                {

                    btn.ChangeCursor(Microsoft.UI.Input.InputSystemCursor.Create(Microsoft.UI.Input.InputSystemCursorShape.Hand));

                });

            };

        };

#endif

        });

        Microsoft.Maui.Handlers.ImageButtonHandler.Mapper.AppendToMapping("Custom", (handler, view) =>

        {

#if WINDOWS

        ImageButton button = handler.VirtualView as ImageButton;

        button.HandlerChanged    += (sender, args) =>

        {

            Microsoft.UI.Xaml.Controls.Button btn = (Microsoft.UI.Xaml.Controls.Button)handler.PlatformView;

            btn.Loaded += (s2, e2) =>

            {

                MainThread.BeginInvokeOnMainThread(() =>

                {

                    btn.ChangeCursor(Microsoft.UI.Input.InputSystemCursor.Create(Microsoft.UI.Input.InputSystemCursorShape.Hand));

                });

            };

        };

#endif

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

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 29,381 Reputation points Microsoft Vendor
    2024-06-12T03:56:06.28+00:00

    Hello,

    The solution in the article is correct, using behavior is a good practice. If you don't want to use behaviors, but instead want to use handlers to make every button on MacCatalyst have this functionality, please refer to the following code:

    Handler:

    (You can put the following code in MauiProgram.cs
    After this line of code #if DEBUG     builder.Logging.AddDebug(); #endif, before this line of code });    return builder.Build();)

    Microsoft.Maui.Handlers.ButtonHandler.Mapper.AppendToMapping("custom", (handler, view) =>
            {
    #if MACCATALYST
                UIButton uIButton = handler.PlatformView as UIButton;// get the platformview 
     
                if (uIButton.GestureRecognizers is not null)
                {
                    foreach (var recognizer in uIButton.GestureRecognizers.OfType<PointerUIHoverGestureRecognizer>())
                    {
                        uIButton.RemoveGestureRecognizer(recognizer);
                    }
                }
     // add the hovergesture (Native maccatalyst API)
                uIButton.AddGestureRecognizer(new PointerUIHoverGestureRecognizer(r =>
                {
                    switch (r.State)
                    {
                        case UIGestureRecognizerState.Began:
                            NSCursor.OpenHandCursor.Set();
                            break;// add the hand
                        case UIGestureRecognizerState.Ended:
                            NSCursor.ArrowCursor.Set();
                            break;
                    }
                }));
     
     
    #endif
    

    Gesture Class

    (Put the following code after MauiProgram class, like this format

    public static class MauiProgram...
    #if MACCATALYST
    public class PointerUIHoverGestureRecognizer...
    #endif 
    

    )

    #if MACCATALYST
    public class PointerUIHoverGestureRecognizer : UIHoverGestureRecognizer
    {
    
    public PointerUIHoverGestureRecognizer(Action<UIHoverGestureRecognizer> action) : base(action)
    {
    }
    }
    #endif
    

    Namespaces

    
    #if MACCATALYST
    using AppKit;
    using UIKit;
    #endif
    
    

    Best Regards,

    Wenyan Zhang


    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.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,731 Reputation points
    2024-05-30T15:56:06.0533333+00:00
    1 person found this answer helpful.