Created
January 13, 2021 09:53
-
-
Save mr5z/f48da58aa880871b085997475913c85e to your computer and use it in GitHub Desktop.
DelegateCommand wrapper
This file contains 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
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