AvaloniaUI+ReactiveUI中执行异步命令时,界面冻结。
创始人
2024-11-12 19:00:39
0

使用Command来执行异步操作,并在Command中实现ICancellableCommand接口来处理取消操作,避免界面冻结。

代码示例:

public class AsyncCommand : ICommand, ICancellableCommand
{
    private readonly Func _execute;
    private bool _isExecuting;

    public AsyncCommand(Func execute)
    {
        _execute = execute;
    }

    public bool CanExecute(object parameter)
    {
        return !_isExecuting;
    }

    public async void Execute(object parameter)
    {
        _isExecuting = true;
        try
        {
            await _execute();
        }
        finally
        {
            _isExecuting = false;
        }
    }

    public void Cancel()
    {
        // TODO: Implement cancellation
    }

    public event EventHandler CanExecuteChanged;
}

使用方法:

private AsyncCommand _asyncCommand;

public ViewModel()
{
    _asyncCommand = new AsyncCommand(ExecuteAsync);
}

public ICommand AsyncCommand => _asyncCommand;

private async Task ExecuteAsync()
{
    // TODO: Implement asynchronous operation
}

相关内容

热门资讯

让我来分享经验!sohoo开挂... 让我来分享经验!sohoo开挂辅助(辅助挂)原来真的是有挂(有挂秘笈);1、全新机制【sohoo开挂...
盘点一款!wejoker私人辅... 盘点一款!wejoker私人辅助软件(辅助挂)一直有挂(今日头条)wejoker私人辅助软件是不是有...
一分钟带你了解!wepoker... 一分钟带你了解!wepoker有透视吗(辅助挂)确实是有挂(有挂技术);1、上手简单,内置详细流程视...
交流学习经验!wepoker怎... 交流学习经验!wepoker怎么看底牌(辅助挂)都是有挂(竟然有挂)1、下载好wepoker怎么看底...
每日必看教程!wepoker有... 每日必看教程!wepoker有透视底牌吗(辅助挂)原来是真的有挂(有挂教学)在进入wepoker有透...
推荐一款!wepoker私人局... 推荐一款!wepoker私人局外卦(辅助挂)真是真的是有挂(有挂透视)1、wepoker私人局外卦辅...
盘点几款!wepoker透视破... 盘点几款!wepoker透视破解版(辅助挂)真是有挂(有挂方略)1、wepoker透视破解版破解器简...
终于懂了!wepoker破解是... 您好,wepoker破解是真的还是假的这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670...
玩家必备教程!pokerwor... 玩家必备教程!pokerworld软件(辅助挂)一直是真的有挂(果真有挂)1、让任何用户在无需pok...
实测分享!德普之星的辅助工具介... 实测分享!德普之星的辅助工具介绍(辅助挂)果然是有挂(有挂规律)1、全新机制【德普之星的辅助工具介绍...