【问题标题】:Determine which visual element was long-pressed in TouchEffect? - Xamarin Community Toolkit确定 TouchEffect 中哪个视觉元素被长按? - Xamarin 社区工具包
【发布时间】:2021-11-05 05:26:03
【问题描述】:

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

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 的子类。)

[编辑:索引已更正]

【问题讨论】:

  • 使用命令参数
  • @Jason CommandParameter 将一个视觉元素作为其第一个参数,但我们不知道哪个视觉元素是鸡和蛋?
  • 也将 VisualElement 作为第二个参数传递
  • @Jason 谢谢 - 会检查的。
  • choiceButtons[i].Text 替换为choiceButtons[i]

标签: xamarin.forms xamarin-community-toolkit


【解决方案1】:

这是我找到的解决方案。感谢 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-05-06
    • 2021-10-12
    • 2021-09-21
    • 2021-10-08
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    相关资源
    最近更新 更多