2016-03-01 105 views
2

嘿,我正在工作Xamarin Forms,我正在處理android雙擊的問題。禁用Android雙擊Xamarin Forms標籤

我的問題是我使用標籤作爲按鈕 - 當我點擊這個快速,應用程序將崩潰。我想通過點擊後禁用點擊來阻止這種情況。

我的標籤在XAML定義是這樣的:

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

我的後臺代碼是這樣的:

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

回答

2

好了,你可以有一個外部的布爾避免(也,不知道,但暫時禁用標籤可以工作):

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

    } 
0

在Android上,UI註冊多個水龍頭和隊列起來執行一前一後。因此,雙擊按鈕可以執行兩次命令並導致意外行爲。最簡單的方法是讓你的命令觀察一個布爾屬性並打開/關閉該屬性。事情是這樣的,

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/

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

有了這個解決方案,你仍然會得到多個水龍頭噪音。但這是一個不同的問題,對吧?