Skip to content

Instantly share code, notes, and snippets.

@mr5z
Created January 13, 2021 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mr5z/f48da58aa880871b085997475913c85e to your computer and use it in GitHub Desktop.
Save mr5z/f48da58aa880871b085997475913c85e to your computer and use it in GitHub Desktop.
DelegateCommand wrapper
public class BaseCommand : DelegateCommand
{
public double InvocationDelayInSeconds { get; set; }
private DateTime lastInvokeTime = DateTime.MinValue;
private readonly Func<bool> canExecute;
public BaseCommand(
Action executeAction,
double invocationDelayInSeconds = 1)
: base(executeAction)
{
InvocationDelayInSeconds = invocationDelayInSeconds;
}
public BaseCommand(
Action executeAction,
Func<bool> canExecute,
double invocationDelayInSeconds = 1)
: base(executeAction, canExecute)
{
this.canExecute = canExecute;
InvocationDelayInSeconds = invocationDelayInSeconds;
}
protected override void Execute(object parameter)
{
if (DateTime.Now - lastInvokeTime < TimeSpan.FromSeconds(InvocationDelayInSeconds))
return;
if (IsActive || (canExecute != null && !canExecute()))
return;
IsActive = true;
base.Execute(parameter);
IsActive = false;
lastInvokeTime = DateTime.Now;
}
protected override void OnIsActiveChanged()
{
base.OnIsActiveChanged();
RaiseCanExecuteChanged();
}
protected override bool CanExecute(object parameter)
{
return !IsActive;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment