Maui - hover button -hand like in css in net 6 using a global handler mapping

Dani_S 5,581 Reputation points
2023-04-24T17:26:17.13+00:00

Hi, If possible to create sample of hover button -hand like in css in net 6 using a global handler mapping?

Developer technologies | .NET | .NET MAUI
{count} votes

Answer accepted by question author
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,451 Reputation points Microsoft External Staff
    2023-04-25T05:33:25.0133333+00:00

    Hello,

    This thread is an extra question of Maui - hover button I see hand like in css. Your main purpose is to know if you don't need to add the code for each button in app.

    You can find the VirtualView and customize the Cursor in handler mapping.

    (You cannot set ProtectedCursor until the Visual Tree is loaded. It's the limitation in WinUI, see Changing the mouse cursor in WinUI 3 #1816)

    Microsoft.Maui.Handlers.ButtonHandler.Mapper.AppendToMapping("Custom", (handler, view) =>
            {
    #if WINDOWS
                Button button = handler.VirtualView as Button;
                button.Loaded += (sender, args) =>
                {
                    Microsoft.UI.Xaml.Controls.Button btn = (Microsoft.UI.Xaml.Controls.Button)handler.PlatformView;
                    MainThread.BeginInvokeOnMainThread(() =>
                    {
                        ElementExtension.ChangeCursor(btn, InputSystemCursor.Create(InputSystemCursorShape.Hand));
                    });
                };                  
    #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.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.