确定哪个视觉元素在 TouchEffect 中被长按? - Xamarin 社区工具包

Determine which visual element was long-pressed in TouchEffect? - Xamarin Community Toolkit

我在页面上有一组视觉元素,其中任何一个都可以长按。在代码中,当长按按钮时,我可以获得 运行 的命令:

for (int i = 0; i < choiceButtons.Length; i++)
{
    TouchEffect.SetLongPressCommand(choiceButtons[i], new Command( async () =>
    {
        // Do stuff here, depending which button was long-pressed
    }));
    TouchEffect.SetLongPressCommandParameter(choiceButtons[i], choiceButtons[i].Text);
}

但是,我需要能够确定长按的是哪个视觉元素。有办法吗?

(视觉元素是网格的子类。)

[编辑:更正索引]

这是我找到的解决方案。感谢 Jason 和 ColeX。

for (int i=0; i < choiceButtons.Length; i++)
{
    TouchEffect.SetLongPressCommand(choiceButtons[i], new Command(async () =>
    {
        // Here when a button is long-pressed
        object obj;
        for (int j=0; j < choiceButtons.Length; j++)
        {
            if (TouchEffect.GetState(choiceButtons [j] ) != TouchState.Pressed)
                continue;
            obj = TouchEffect.GetLongPressCommandParameter(choiceButtons[j]);
            if (obj != null)
            {
                MyButton btn = (MyButton)obj;  // This is the button that was long-pressed
                await HandleLongPressAsync (btn);   // Do stuff
                break;
            } 
    }));
    TouchEffect.SetLongPressCommandParameter(choiceButtons[i], choiceButtons[i]);
}