禁用 Android 双击 Xamarin 表单标签

ste*_*eaw 4 c# xaml android gesture-recognition xamarin.forms

嘿,我正在使用 Xamarin Forms,我正在处理 android 双击问题。

我的问题是我使用标签作为按钮 - 当我快速单击它时,应用程序将崩溃。我想通过在单击后禁用点击来防止这种情况发生。

我的标签在 XAML 中定义如下:

<Label x:Name="LabelName" Text="LabelText"/>
Run Code Online (Sandbox Code Playgroud)

我的代码隐藏是这样的:

LabelName.GestureRecognizers.Add((new TapGestureRecognizer
{
  Command = new Command(async o =>
  {
    await Navigation.PopToRootAsync();
  })
}));
Run Code Online (Sandbox Code Playgroud)

Gus*_*man 5

好吧,您可以使用外部布尔值来避免这种情况(也不确定,但暂时禁用 Label 可能会起作用):

//On the form, so you can use a reference to This, else this is a value variable and will be copied and false always
bool disable = false; 
Run Code Online (Sandbox Code Playgroud)

进而:

LabelName.GestureRecognizers.Add((new TapGestureRecognizer
{
  Command = new Command(async o =>
  {
     if(this.disable)
       return;

     this.disable = true;

    await Navigation.PopToRootAsync();

    this.disable = false;
 })
}));
Run Code Online (Sandbox Code Playgroud)