xamarin 如何检测GraphicsView中绘制元素上的单击

7xzttuei  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(100)

我创建了一个IDrawable,它在GraphicsView中绘制不同的圆弧,以便创建饼图。
到目前为止没有问题,除了现在我想检测这些圆圈部分中的一个的点击或悬停。但是我没有看到任何东西可以让我轻松地做到这一点。

即使从View继承我的IDrawable来附加一个tappgesture到它似乎也不起作用(即使这样,它也不会真正回答我的问题,即能够知道哪个部分被点击了)。

public class PieChartDrawable : View, IDrawable
    {
        private readonly PieChart _chart;

        public PieChartDrawable(PieChart chart) 
        { 
            _chart = chart;

            var tapGestureRecognizer = new TapGestureRecognizer();

            tapGestureRecognizer.Tapped += OnTapped;

            GestureRecognizers.Add(tapGestureRecognizer);
        }

        private void OnTapped(object sender, EventArgs e)
        {
            // Does not trigger.
        }

        public void Draw(ICanvas canvas, RectF dirtyRect)
        {
            if (_chart.Entries == null)
                return;

            [...]
          
        } 
    }

是我做错了,还是有什么方法可以做到?(我正在寻找一个MAUI的解决方案,但我想在Xamarin Forms下工作的东西会做得很好)。
先谢谢你。

sh7euo9m

sh7euo9m1#

It seams it can be a solution from the first sight:

  1. Use the solution described in this one to detect the point
  2. Write the function PointInPolygon to detect the selected area (Point in the big circle, Point out of small circle and angle to detect sector)
    Other one I have found at the moment the more complex to quick reuse :)

ADDED:

Invoking Events from Effects

相关问题