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.