.net Maui中的ASYNCCOMMAND

发布于 2025-01-29 23:01:20 字数 84 浏览 5 评论 0原文

我似乎找不到.net Maui或.Net Maui社区工具包中的asynccommand。知道我能找到哪个软件包/名称空间?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ま柒月 2025-02-05 23:01:20

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

.NET MAUI工具包将不包含Xamarin的MVVM功能
社区工具包,例如Asynccommand。展望未来,我们将添加
所有MVVM规格的功能
communityToolkit.mvvm。

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.

优雅的叶子 2025-02-05 23:01:20

即使问题已被标记为解决,也可能会从约翰·蒂里特(John Thiriet)撰写的这种解决方案中获利。我实施了它,它运行良好。
https://johnthiriet.com/moveriet.com/mvvm-mmvvm-onemvvm-oneganc-wosh-yanc-wosh-onganc-oneganc-wosh-yand-andas-yanc-wonc-with-ashync-with-ashync-async-ashync- -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
}

然后,它可以像Xamarin一样在MAUI中使用。

public MyMVVM()
{
   MyCommand = new AsyncCommand(async()=> await MyMethod);
}
...
public AsynCommand MyCommand {get;}

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;}
瑾夏年华 2025-02-05 23:01:20

安装CommunityToolKitMvvm 8.0.0

[RelayCommand]
async Task your_method (){...}

install CommunityToolkitMVVM 8.0.0

[RelayCommand]
async Task your_method (){...}
苍风燃霜 2025-02-05 23:01:20

将asyncawaitbestpractices.mvvm nuget软件包添加到您的项目中,以使asynccommand返回。

有关更多信息,请参见GitHub项目页面: https://github.com/github.com/github.com/brminnick/brminnick/asyncawaitbestpractices

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

紫﹏色ふ单纯 2025-02-05 23:01:20
  1. 安装Nuget软件包communityToolkit.mvvm。
  2. 声明IASYNCRELAYCOMMAND并创建一个AsyncrelayCommand的实例,就像这样:

public iaSyncrelayCommand scancommand =&gt; this.scancommand ?? =新的asyncrelayCommand(this.scannetworksasync);

  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);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文