MAUI GraphicsView鼠标点击事件

Csa*_*aba 5 maui

我是.Net Maui的新手。尝试使用GraphicsView和绘制我自己的形状IDrawable。我想让形状可点击。

有办法实现吗?也许还有另一个IClickable我不知道的界面。或者我应该使用与GraphicsView.

谢谢 :-)

小智 2

我能够使用TapGestureRecognizer和一个实现 的类来使其工作ICommand

在此示例中,我在实现和的同一个类中GraphicsView调用了,但您的设计可能有所不同,但它应该仍然有效。_viewIDrawableICommand

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
    }
}
Run Code Online (Sandbox Code Playgroud)