Skip to content

Instantly share code, notes, and snippets.

@programmation
Created June 21, 2015 23:20
Show Gist options
  • Save programmation/eee9c797b05f585029b0 to your computer and use it in GitHub Desktop.
Save programmation/eee9c797b05f585029b0 to your computer and use it in GitHub Desktop.
How to delay response to user text entry until after user finishes typing
// http://forums.xamarin.com/discussion/comment/133731/#Comment_133731
public partial class Page1
{
public Page1()
{
this.InitializeComponent();
this.Entry.TextChanged += this.EntryOnTextChanged;
}
//
private CancellationTokenSource throttleCts = new CancellationTokenSource();
private void EntryOnTextChanged(object sender, TextChangedEventArgs args)
{
Interlocked.Exchange(ref this.throttleCts, new CancellationTokenSource()).Cancel();
Task.Delay(TimeSpan.FromMilliseconds(500), this.throttleCts.Token) // throttle time
.ContinueWith(
delegate { this.HandleString(this.Entry.Text); },
CancellationToken.None,
TaskContinuationOptions.OnlyOnRanToCompletion,
TaskScheduler.FromCurrentSynchronizationContext());
}
//
private void HandleString(string str)
{
// .. if CanExecute(...) Execute(...)
this.Label.Text = str.Length.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment