Home > other >  Change cursor symbol to hand when image/button focused in mac
Change cursor symbol to hand when image/button focused in mac

Time:03-03

I am developing desktop application in mac using Xamarin forms, in that when I focused on Image/Button I need to change cursor symbol to hand symbol. I googled it but I don't found any solution to solve this. Could you please help me to solve this.

CodePudding user response:

You can do this by adding Triggers to style as follows:

<Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Cursor" Value="Hand"/>
    </Trigger>
</Style.Triggers>    

CodePudding user response:

We can define a custom NSButton/NSImageView class ,handle the mouse hover event and set it in custom Renderer .

The following code works on my side .

[assembly:ExportRenderer(typeof(Button),typeof(MyRenderer))]
namespace ColeForms.macOS
{

    public class myButton : NSButton
    {

        public override void ResetCursorRects()
        {
            base.ResetCursorRects();
            //change cursor style to hand shape while hovering on the view
            AddCursorRect(Bounds, NSCursor.PointingHandCursor); 
        }
    }

    public class MyRenderer : ButtonRenderer
    {

        protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
        {
            base.OnElementChanged(e);

            if(Control != null)
            {
                myButton button = new myButton();
                button.Title = Element.Text;
                button.Cell.Bordered = false;
                button.Cell.BackgroundColor = Element.BackgroundColor.ToNSColor();
                SetNativeControl(button);
            }           
        }
    }
}
  • Related