Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Asynchronous Commands

David Sungaila edited this page Sep 18, 2020 · 1 revision

Define a class, derive from PresentationBase.ViewModelCommandAsync<> and pass your ViewModel as the generic type.

Implement ExecutionAsync in the async-await-pattern.

CanExecute can be overridden just like in normal commands.

public class AlertCommandAsync : ViewModelCommandAsync<AwesomeViewModel>
{
    protected override async Task ExecutionAsync(AwesomeViewModel parameter)
    {
        await Task.Run(() =>
        {
            System.Threading.Thread.Sleep(2000);
            System.Windows.MessageBox.Show("You clicked that button two seconds ago.");
        });
    }
}

Call ExecuteAsync to execute your command asynchronously.

Calling Execute executes your command in a fire and forget kind of way. You will have to override HandleUncaughtException, if you wish for thrown exceptions to be handled.