0

In Xamarin forms we have delegate commands like this:

        /// Gets or sets the logout command.
        /// </summary>
        /// <value>
        /// The logout command.
        /// </value>
        public DelegateCommand LogoutCommand { get; set; }

Which is initialized in the constructor like this:

this.LogoutCommand = new DelegateCommand(this.ExecuteLogoutCommand);

Currently, the ExecuteLogoutCommand is return void type, but I wish to make it Task as it has an async task. But doing so breaks the initialization in the constructor. Any help here?

Update The following link helped me: https://johnthiriet.com/mvvm-going-async-with-async-command/

But now I get this error in my Unit Tests:

MobileApp.Core.UnitTests.Add.ConfirmationPageViewModelUnitTests.OnNavigatedToBackBehavior threw exception: System.MissingMethodException: Method not found: 'MobileApp.Core.IServices.IAsyncDelegateCommand MobileApp.Core.ViewModel.Add.ConfirmationPageViewModel.get_GoBackHomeCommand()'
  • Does this answer your question? [How to use async method in DelegateCommand](https://stackoverflow.com/questions/43167307/how-to-use-async-method-in-delegatecommand) – Mihail Duchev Jul 08 '20 at 11:51
  • @mihailduchev No, I tried this. couldn't get it to work. – priya bhandari Jul 08 '20 at 11:56
  • What exactly have you tried? – Mihail Duchev Jul 08 '20 at 11:58
  • @mihailduchev I wish to override the delegate command method to have an option of returning task method. Is that possible? – priya bhandari Jul 08 '20 at 12:42
  • https://johnthiriet.com/mvvm-going-async-with-async-command/ This article help me a lot – Nenad Jul 08 '20 at 14:18
  • Why not use a new command(the same function of ExecuteLogoutCommand) with return type task? – nevermore Jul 09 '20 at 05:29
  • @Nenad, Thanks for that link, that helps. Although I get this error in my tests now: Test method MobileApp.Core.UnitTests.Add.ConfirmationPageViewModelUnitTests.OnNavigatedToBackBehavior threw exception: System.MissingMethodException: Method not found: 'MobileApp.Core.IServices.IAsyncDelegateCommand MobileApp.Core.ViewModel.Add.ConfirmationPageViewModel.get_GoBackHomeCommand()'. – priya bhandari Jul 13 '20 at 11:49

0 Answers0