Created
May 3, 2017 08:55
-
-
Save rhishikeshj/9a4bfda21204b8e0abb7af964985fc08 to your computer and use it in GitHub Desktop.
How to debounce an Entry control in Xamarin
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
private Task _debounceTask; | |
public ExtendedEntry() | |
{ | |
int debounceDelay = 500; | |
CancellationTokenSource _debounceTaskCancellationSource = null; | |
TextChanged += (sender, e) => | |
{ | |
if (_debounceTask != null) | |
{ | |
_debounceTaskCancellationSource.Cancel(); | |
_debounceTaskCancellationSource.Dispose(); | |
_debounceTask = null; | |
} | |
var text = Text; | |
_debounceTaskCancellationSource = new CancellationTokenSource(); | |
_debounceTask = Task.Delay(debounceDelay, _debounceTaskCancellationSource.Token).ContinueWith((task) => | |
{ | |
if (_debounceTaskCancellationSource.IsCancellationRequested == false) | |
{ | |
if (text.Equals(Text)) | |
{ | |
DebouncedTextChanged.Invoke(sender, new TextChangedEventArgs(text, Text)); | |
} | |
} | |
_debounceTask = null; | |
_debounceTaskCancellationSource.Dispose(); | |
}); | |
}; | |
} | |
public event EventHandler<TextChangedEventArgs> DebouncedTextChanged; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do use in xamarin forms?