禁用 Xamarin Forms 标签上的 Android 双击功能

5

嘿,我正在使用Xamarin Forms进行开发,我遇到了Android双击的问题。

我的问题是,我正在使用一个标签作为按钮 - 当我快速点击它时,应用程序会崩溃。我希望通过在单击后禁用轻按来解决这个问题。

我的Label在XAML中定义如下:

<Label x:Name="LabelName" Text="LabelText"/>

我的代码后台是这样的:

LabelName.GestureRecognizers.Add((new TapGestureRecognizer
{
  Command = new Command(async o =>
  {
    await Navigation.PopToRootAsync();
  })
}));

使用“NumberOfTapRequired”属性。 - sameer
@sameer,当设置为1时,这并没有帮助。 - maf-soft
7个回答

6

好的,您可以使用一个外部布尔变量来避免这种情况(另外,不确定但是暂时禁用标签可能也可以起作用):

//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; 

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

     this.disable = true;

    await Navigation.PopToRootAsync();

    this.disable = false;
 })
}));

4

在Android上,UI会注册多次点击并将它们排队以便一个接一个地执行。因此,双击按钮可能会执行两次命令并导致意外行为。最简单的方法是让您的命令观察一个bool属性并切换该属性的开/关状态。像这样:

SomeCommand = new Command (OnCommand,(x)=> CanNavigate);

async void OnCommand (object obj)
{
        CanNavigate = false;

        await CurrentPage.DisplayAlert ("Hello", "From intelliAbb", "OK");

        CanNavigate = true;

}

你可以在https://intelliabb.com/2017/02/18/handling-multiple-taps-in-xamarin-forms-on-android/查看完整示例。

3
最简单的方法是禁用触发触摸事件的元素。
您可以按照以下示例实现:
```html

点击此处不会触发事件。

```
var tapRecognizer = new TapGestureRecognizer();
tapRecognizer.Tapped += async (s,e) => 
{
   ((View)s).IsEnabled = false; // or reference the view directly if you have access to it
   await Navigation.PopToRootAsync();
   ((View)s).IsEnabled = true;
};
theLabel.GestureRecognizers.Add(tapRecognizer);

如果在其中某个地方抛出异常并且您的启用为真从未被调用,会发生什么? - FreakyAli
你可以将其放在try块中,以确保安全。 - chaosifier
如果你问先生,那么你应该在这里处理好了。 - FreakyAli

0

以下是我在C#中的做法:

private static object _tappedLockObject = new object();
private static bool _tapped = false;

private void tapHandler()
{
    // one-at-a-time access to this block prevents duplicate concurrent requests:
    lock(_tappedLockObject)
    {
        if(_tapped) return;
        _tapped = true;
    }
    handleTap();
}

private void reenableTap()
{
    _tapped = false;
}

使用这个解决方案,您仍会听到多次点击噪音。但这是另一个问题,对吧?


0

点击按钮时,只需检查应用程序是否繁忙

 if (IsBusy)
            return;

0

在我的情况下,使用 allowsMultipleExecutions: falseAsyncCommand 有所帮助:

public ICommand TappedCommand => _tappedCommand ?? (_tappedCommand = 
    new AsyncCommand(() => Navigation.PopAsync(), allowsMultipleExecutions: false));

0

你需要创建一个操作来禁用你的视图。你可以添加可配置的超时时间来禁用它。 你可以为点击或视图实现它,并添加任何其他方法来控制按压。 你的代码应该像这样:

public abstract class ThrottlingListener : Java.Lang.Object
    {
        readonly TimeSpan timeout;

        protected ThrottlingListener( TimeSpan timeout = default(TimeSpan))
        {
            this.timeout = timeout == TimeSpan.Zero ? TimeSpan.FromSeconds(1) : timeout;
        }

        protected bool IsThrottling()
        {
            var now = DateTime.UtcNow;
            if (now - LastClick < timeout)
            {
                return true;
            }
            LastClick = now;
            return false;
        }

        protected DateTime LastClick{ get; private set;}

        protected void DisableView(View view)
        {
            view.Enabled = false;
            view.PostDelayed (() => 
            {
                view.Enabled = true;
            }, (long)timeout.TotalMilliseconds);
        }
    }

    public class ThrottlingOnClickListener : ThrottlingListener, View.IOnClickListener
    {
        readonly Action onClick;

        public ThrottlingOnClickListener(Action onClick, TimeSpan timeout = default(TimeSpan)) : base(timeout)
        {
            this.onClick = onClick;
        }       

        public void OnClick(View view)
        {
            if (IsThrottling())
                return;

            DisableView (view);
            onClick ();
        }

    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接