DelegateCommandBase Execute/CanExecute

Dec 4, 2013 at 9:20 AM
I was wondering why DelegateCommand doesn't maintain an _isExecuting flag and RaiseCanExecuteChanged.

Something like this:-
        /// <summary>
        /// Executes the command with the provided parameter by invoking the <see cref="Action{Object}"/> supplied during construction.
        /// </summary>
        /// <param name="parameter"></param>
        protected async Task Execute(object parameter)
        {
            try
            {
                _isExecuting = true;
                RaiseCanExecuteChanged();
                await _executeMethod(parameter);
            }
            finally
            {
                _isExecuting = false;
                RaiseCanExecuteChanged();
            }
        }

        /// <summary>
        /// Determines if the command can execute with the provided parameter by invoing the <see cref="Func{Object,Bool}"/> supplied during construction.
        /// </summary>
        /// <param name="parameter">The parameter to use when determining if this command can execute.</param>
        /// <returns>Returns <see langword="true"/> if the command can execute.  <see langword="False"/> otherwise.</returns>
        protected bool CanExecute(object parameter)
        {
            return !_isExecuting && (_canExecuteMethod == null || _canExecuteMethod(parameter));
        }
Thanks
--Michael
Editor
Dec 4, 2013 at 4:12 PM
Hi Michael,

What would be the objective of the _isExecuting member you are describing? The RaiseCanExecuteChanged method is implemented on the DelegateCommandBase class to raise the CanExecuteChanged event. Do you have a scenario where it would be useful to raise this event during the execution of the command?

Thanks,

Damian Cherubini
http://blogs.southworks.net/dcherubini
Dec 4, 2013 at 4:38 PM
Hi Damian,

If the command is bound to a Button, I want to disable the button while the command is executing. If I don't, the user could click Save or Delete or whatever a second time before the first command has finished executing. At the moment, I have to implement the same logic at the ViewModel level for every command which just seems unnecessary.

I can't actually think of a scenario where one wouldn't want to disable the button while the command is executing. Or am I missing something here?

--Michael
Editor
Dec 4, 2013 at 5:50 PM
Hi,

Indeed, what you describe seems to be a common scenario when the command is bound to a button. However, a command can be used with other controls too, where some users might not want this kind of behavior enabled by default (perhaps this could be turned on and off using a property in the command, etc.) As far as I know, the current implementation of the DelegateCommand class (and its base class) was ported from the WPF version of the library, which doesn't have this feature you are mentioning either.

You can create a work item in the Issues section as a feature request to include support for this as out-of-the-box in the library, including any code and suggestions you might want to add. This will allow the developers to analyze your suggestion and possibly include it in further releases of the project if they see it fits.

Thanks!

Damian Cherubini
http://blogs.southworks.net/dcherubini
Dec 4, 2013 at 6:15 PM
Hi,

I'll create a work item.

I'd still be interested to hear if anyone else reading this thread can think of a scenario where you would want to allow re-entrancy into a currently executing command. I would have thought the behaviour would be undefined almost by definition. Of course, before the world of Async/await this wouldn't have been an issue.

Many thanks,
--Michael