4

Assuming I have the following command

public class SignOutCommand : CommandBase, ISignOutCommand
{
    private readonly UserModel _userModel;
    private readonly IUserService _userService;

    public SignOutCommand(IUserService userService)
    {
        _userService = userService;
        _userModel = App.CurrentUser;
    }

    public bool CanExecute(object parameter)
    {
        var vm = parameter as EditProfileViewModel;
        return vm != null;
    }

    public Task<bool> CanExecuteAsync(object parameter)
    {
        return Task.Run(() => CanExecute(parameter);
    }

    public void Execute(object parameter)
    {
        var vm = (EditProfileViewModel)parameter;
        var signOutSucceeded = SignOutUser();

        if (signOutSucceeded)
        {
            vm.AfterSuccessfulSignOut();
        }
    }

    public Task ExecuteAsync(object parameter)
    {
        return Task.Run(() => Execute(parameter);
    }

    private bool SignOutUser()
    {
        // populate this so that we get the attached collections.
        var user = _userService.GetByEmailAddress(_userModel.Email);
        _userService.RevokeLoggedInUser(user);
        return true;
    }
}

And my XAML has a button

        <Button Text="Sign out"
                Command="{Binding SignOutCommand}"
                CommandParameter="{Binding}" />

What would it take for this to execute the ExecuteAsync instead of Execute? Sorry if this is trivial, I'm quite new to XAML.

Also, I'm actually doing this in Xamarin.Forms XAML, not sure if it makes a difference here.

Chase Florell
  • 46,378
  • 57
  • 186
  • 376
  • Have you tried just like he did here? http://stackoverflow.com/questions/12858501/is-it-possible-to-await-an-event-instead-of-another-async-method – VixinG Aug 14 '14 at 23:02
  • have you tried this way? http://www.wintellect.com/blogs/jlikness/asynchronous-commands-in-windows-store-apps – mimipofi Jan 09 '15 at 09:39

1 Answers1

4

You could simply implement your command this way

public class SignOutCommand : ICommand
{
    public bool CanExecute(object parameter)
    {
        var vm = parameter as EditProfileViewModel;
        return vm != null;
    }

    public async void Execute(object parameter)
    {
        Task.Run(() => 
        {
            var vm = (EditProfileViewModel)parameter;
            var signOutSucceeded = SignOutUser();

            if (signOutSucceeded)
            {
                vm.AfterSuccessfulSignOut();
            }
        }
    }

    ...
}

But then the bound button is not disabled during execution and the command can be executed even if it is already running...

If you need that the command cannot be executed during execution have a look at:

Implementing the CanExecute asynchronously would be more tricky...

Rico Suter
  • 11,548
  • 6
  • 67
  • 93
  • I've done something similar, and yes I can disable the button without issue. I'm just wondering if there's a way to execute the Async version of the method. – Chase Florell Aug 15 '14 at 15:55