DelegateCommand with a async Task method.

Feb 27, 2014 at 6:29 PM
How can I use a DelegaetCommand with a async Task Method?

Here my example:

public MyClass()
{
SaveCommand = new DelegateCommand(Save, CanSave);
}
   public virtual async Task Save()
    { }

    public virtual bool CanSave()
    {
        return true;
    }
Thank you,
Manullino
Feb 28, 2014 at 6:51 PM
Hi Manullino,

You can use the static method DelegateCommand.FromAsyncHandler. In your case, it would be:
SaveCommand = DelegateCommand.FromAsyncHandler(Save, CanSave);
Hope that helps,
Jeric
Jul 22, 2014 at 12:45 PM
What exactly is the benefit from this? As opposed to using:
public virtual async void Save()
    { }
I know it's not best practice to do this. Are there advantages in the UI from this?
Jul 23, 2014 at 7:09 AM
There was a big article about that question a while ago (avoid async void) here: http://msdn.microsoft.com/en-us/magazine/jj991977.aspx
hope that answers your question.
Jul 24, 2014 at 2:34 PM
Thanks, I understand the benefits of avoiding async void but was just wondering about the particular benefits in this situation, i.e. on the DelegateCommand? As this is just like an EventHandler in effect. Nothing still is awaiting the finish of this task? Is it?
Jul 29, 2014 at 12:37 AM
Edited Jul 29, 2014 at 12:39 AM
As mentioned in that article linked above, the exception for void async methods are event handlers. Since you are using a command, you should just use async Task. The actual method is treated as an event handler behind the scenes, but as far as the view model is concerned, it's just another method.