ICommand implementation with a little bit of async in WinRT

So the RelayCommand or DelegateCommand (whichever name you prefer) is a well known chap.  We created an AsyncRelayCommand which essentially disables the command while an async action is running.  This is useful because commands may call services (which are async) and during that call you’d rather the user not do it again.

This is nothing new, but it is a cool example of how easy stuff like this is now you have the async keyword.  It’s also been rather handy to sprinkle the async commands around our code as it simplifies the can execute logic.

Here’s the code:

public class AsyncRelayCommand : IAsyncCommand
    {
        readonly Func<object, bool> _canExecute;
        bool _isExecuting;
        readonly Func<object, Task> _action;

        public event EventHandler CanExecuteChanged;

        public AsyncRelayCommand(Func<object, Task> action)
        {
            _action = action;
        }

        public AsyncRelayCommand(Func<object, Task> action, Func<object, bool> canExecute)
            : this(action)
        {
            _canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            var canExecutResult = _canExecute == null || _canExecute(parameter);
            return !_isExecuting && canExecutResult;
        }

        async void ICommand.Execute(object parameter)
        {
            await Execute(parameter);
        }

        public async Task Execute(object parameter)
        {
            _ChangeIsExecuting(true);
            try
            {
                await _action(parameter);
            }
            finally
            {
                _ChangeIsExecuting(false);
            }
        }

        private void _ChangeIsExecuting(bool newValue)
        {
            if (newValue == _isExecuting)
            {
                return;
            }
            _isExecuting = newValue;
            _OnCanExecuteChanged();
        }

        void _OnCanExecuteChanged()
        {
            var handler = CanExecuteChanged;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }
    }
Advertisements

About Tom Peplow

C# .Net developer based in London and the South Coast
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

7 Responses to ICommand implementation with a little bit of async in WinRT

  1. Pingback: Windows 8 Developer Links – 2012-10-30 | Dan Rigby

  2. Pingback: ICommand implementation with a little bit of async in WinRT

  3. olivier says:

    Where is the definition of IAsyncCommand please ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s