Skip to content

Instantly share code, notes, and snippets.

@dimitrispaxinos
Last active August 29, 2015 14:19
Show Gist options
  • Save dimitrispaxinos/97be51bfcd766fc174d5 to your computer and use it in GitHub Desktop.
Save dimitrispaxinos/97be51bfcd766fc174d5 to your computer and use it in GitHub Desktop.
/// <summary>
/// DisableableCommandAsync Constructor
/// </summary>
/// <param name="asyncExecute">Executing Delegate</param>
/// <param name="canExecute">Predicate for enabling/disabling</param>
/// <param name="disablables">List of IDisaeable Items to be disabled while executing</param>
/// <param name="disableWhileInProgress">Disable the Command itself while executing</param>
public DisableableCommandAsync(Func<object,Task> asyncExecute,
Predicate<object> canExecute,
IEnumerable<IDisableableCommand> disablables = null,
bool disableWhileInProgress = false)
: this(asyncExecute, canExecute)
{
if (disablables != null)
{
_disablables = disablables.ToList();
if (disableWhileInProgress)
_disablables.Add(this);
}
}
public async void Execute(object parameter)
{
if (_disablables != null)
{
// disable IDisableable items while executing
using (var scope = new DisablingScope(_disablables))
{
await ExecuteAsync(parameter);
}
CommandManager.InvalidateRequerySuggested();
}
else
await ExecuteAsync(parameter);
}
protected virtual async Task ExecuteAsync(object parameter)
{
await _asyncExecute(parameter);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment