0

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

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);
}

但是,我需要能够确定哪个视觉元素是长按的。有没有办法做到这一点?

(视觉元素是 Grid 的子类。)

[编辑:索引更正]

4

1 回答 1

0

这是我找到的解决方案。感谢 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]);
}

于 2021-09-09T14:08:04.493 回答