Skip to content

Instantly share code, notes, and snippets.

@rhishikeshj
Created May 3, 2017 08:55
Show Gist options
  • Save rhishikeshj/9a4bfda21204b8e0abb7af964985fc08 to your computer and use it in GitHub Desktop.
Save rhishikeshj/9a4bfda21204b8e0abb7af964985fc08 to your computer and use it in GitHub Desktop.
How to debounce an Entry control in Xamarin
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;
@luizcherpers
Copy link

luizcherpers commented Jun 22, 2018

How do use in xamarin forms?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment