Xamarin Forms:如何为图像添加单击和双击?

Xamarin Forms: How to add single and double tap for an image?

我正在尝试实现一个小键盘 UI。在小键盘中,+(plus)0(zero) 选项位于单个图像图标上。所以我需要在 UI 上显示 0 用于单击和双击我需要在 [=32= 上显示 + ].

我的代码

<Image 
    Grid.Column="1"
    Source="ic_zero_xx.png"
    Style="{StaticResource KeypadImageStyle}">
    <Image.GestureRecognizers>
        <TapGestureRecognizer
            Tapped="ZeroTapped"
            NumberOfTapsRequired="1">
        </TapGestureRecognizer>
    </Image.GestureRecognizers>
</Image>

private void ZeroTapped(object sender, EventArgs e)
{
    phonenumber_label.Text = "0";
}

如何在一张图片上同时提及 2 个不同的点击?

我想你可以使用计时器如果用户在 150 毫秒内点击两次,那么它就是双击。

int Tapped=0;
private void ZeroTapped(object sender,EventArgs e)
{
    if(Tapped==0)
    {
        Device.StartTime(TimeSpan.FromMillSeconds(150),()=>
        {
            if(Tapped=1)
            {
                //1 tap.
            }
            else
            {
                 //double tap.
            }
            Tapped=0;

            return false;
        }
    }
    else
    {
        Tapped++;
    }
}

我们可以在该图像上添加两个 TapGestureRecognizer,一个用于单击,另一个用于双击。

xaml

 <Image >
     <Image.GestureRecognizers>
         <TapGestureRecognizer Tapped="TapGestureRecognizer_Single" NumberOfTapsRequired="1"/>
         <TapGestureRecognizer Tapped="TapGestureRecognizer_Double" NumberOfTapsRequired="2"/>
     </Image.GestureRecognizers>
 </Image>

代码隐藏

  private void TapGestureRecognizer_Single(object sender, EventArgs e)
  {
      label.Text = "0";
  }

  private void TapGestureRecognizer_Double(object sender, EventArgs e)
  {
      label.Text = "+";
  }