AsyncCommand in .NET MAUI
D

5

6

I can't seem to find AsyncCommand in .NET MAUI or .NET MAUI Community Toolkit. Any idea what package/namespace I can find it?

Drinker answered 17/5, 2022 at 16:4 Comment(0)
B
2

install CommunityToolkitMVVM 8.0.0

[RelayCommand]
async Task your_method (){...}
Bureaucracy answered 2/9, 2022 at 8:51 Comment(0)
J
6

https://devblogs.microsoft.com/dotnet/introducing-the-net-maui-community-toolkit-preview/#what-to-expect-in-net-maui-toolkit

The .NET MAUI Toolkit will not contain the MVVM features from Xamarin Community Toolkit, like AsyncCommand. Going forward, we will be adding all MVVM-specifc features to a new NuGet Package, CommunityToolkit.MVVM.

Jemine answered 17/5, 2022 at 16:7 Comment(4)
I guess it's a better idea to keep it in a separate package. Thank you!Drinker
And looks like they dropped AsyncCommand in favor of AsyncRelayCommandDrinker
It makes more sense to have it there and ultimately directly as part of .NET :)Sinclair
Regarding Memory Management is there anything implemented like can be used for events with the WeakEventManager or is it not necessary using the AsyncCommand?Strathspey
M
3

Even if the question has been marked as solved, someone might profit from this solution written by John Thiriet. I implemented it and it worked fine. https://johnthiriet.com/mvvm-going-async-with-async-command/

public interface IAsyncCommand<T> : ICommand
{
    Task ExecuteAsync(T parameter);
    bool CanExecute(T parameter);
}

public class AsyncCommand<T> : IAsyncCommand<T>
{
    public event EventHandler CanExecuteChanged;

    private bool _isExecuting;
    private readonly Func<T, Task> _execute;
    private readonly Func<T, bool> _canExecute;
    private readonly IErrorHandler _errorHandler;

    public AsyncCommand(Func<T, Task> execute, Func<T, bool> canExecute = null, IErrorHandler errorHandler = null)
    {
        _execute = execute;
        _canExecute = canExecute;
        _errorHandler = errorHandler;
    }

    public bool CanExecute(T parameter)
    {
        return !_isExecuting && (_canExecute?.Invoke(parameter) ?? true);
    }

    public async Task ExecuteAsync(T parameter)
    {
        if (CanExecute(parameter))
        {
            try
            {
                _isExecuting = true;
                await _execute(parameter);
            }
            finally
            {
                _isExecuting = false;
            }
        }

        RaiseCanExecuteChanged();
    }

    public void RaiseCanExecuteChanged()
    {
        CanExecuteChanged?.Invoke(this, EventArgs.Empty);
    }

//#region Explicit implementations
    bool ICommand.CanExecute(object parameter)
    {
        return CanExecute((T)parameter);
    }

    void ICommand.Execute(object parameter)
    {
        ExecuteAsync((T)parameter).FireAndForgetSafeAsync(_errorHandler);
    }
//#endregion
}

Then it can be used in MAUI just like in Xamarin.

public MyMVVM()
{
   MyCommand = new AsyncCommand(async()=> await MyMethod);
}
...
public AsynCommand MyCommand {get;}
Mantellone answered 30/1, 2023 at 22:15 Comment(0)
B
2

install CommunityToolkitMVVM 8.0.0

[RelayCommand]
async Task your_method (){...}
Bureaucracy answered 2/9, 2022 at 8:51 Comment(0)
O
1

Add the AsyncAwaitBestPractices.MVVM Nuget package to your project to get the AsyncCommand back.

For more information see the Github project page: https://github.com/brminnick/AsyncAwaitBestPractices

Outset answered 1/2, 2023 at 23:3 Comment(0)
W
0
  1. Install nuget package CommunityToolkit.Mvvm.
  2. Declare IAsyncRelayCommand and create an instance of AsyncRelayCommand, just like that:

public IAsyncRelayCommand ScanCommand => this.scanCommand ??= new AsyncRelayCommand(this.ScanNetworksAsync);

Warr answered 17/4 at 18:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.