MAUI GraphicsView mouse click event

Viewed 555

I'm new to .Net Maui. Trying to draw my own shapes using GraphicsView and IDrawable. I would like to have the shapes clickable.

Is there a way how to achieve that? Maybe there is another interface something like IClickable I'm not aware of. Or I should use different approach thanGraphicsView.

Thanks :-)

1 Answers

I was able to get this to work using the TapGestureRecognizer and a class that implements ICommand.

In this example I have the GraphicsView as a member called _view in the same class that implements both IDrawable and ICommand, but your design could be different and it should still work.

public class Block : IDrawable, ICommand
{
    protected GraphicsView _view = new GraphicsView();

    public Block()
    {
        _view.Drawable = this;

        _view.GestureRecognizers.Add(new TapGestureRecognizer
        {
            Command = this
        });
    }

    void ICommand.Execute(object cmdObject) 
    {
        // Handle the Clicked event here
    }

    bool ICommand.CanExecute(object cmdObject)
    {
        return true;
    }

    event EventHandler ICommand.CanExecuteChanged
    {
        add { }
        remove { }
    }

    public void Draw(ICanvas canvas, RectF dirtyRect)
    {
        // Draw on canvas here
    }
}
Related