Created
June 21, 2015 23:20
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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