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
}

相关内容

热门资讯

辅助透视!wepoker透视脚... 辅助透视!wepoker透视脚本免费下载,微信小程序微乐陕西挖坑辅助,手段教程(有挂方法)-哔哩哔哩...
必备透视!约局吧德州真的存在透... 必备透视!约局吧德州真的存在透视吗,微乐游戏小程序辅助器免费,秘籍教程(有挂实锤)-哔哩哔哩该软件可...
必备透视!wepoker私人局... 必备透视!wepoker私人局透视,如何提高微乐自建胜率,演示教程(有挂方法)-哔哩哔哩亲,关键说明...
了解透视!德州局怎么透视,微信... 了解透视!德州局怎么透视,微信小程序微乐挂如何获取,机巧教程(有挂攻略)-哔哩哔哩小薇(辅助器软件下...
解密透视!wepoker免费脚... 解密透视!wepoker免费脚本咨询,手机微乐小程序破解器,经验教程(有挂教学)-哔哩哔哩1、实时w...
推荐透视!wepoker透视脚... 推荐透视!wepoker透视脚本网页,微信小程序微乐辅助器真吗,法子教程(有挂详情)-哔哩哔哩推荐透...
开挂透视!wejoker辅助机... 开挂透视!wejoker辅助机器人,微乐小程序有脚本吗,方针教程(有挂方法)-哔哩哔哩1、下载好we...
详情透视!约局吧德州真的有透视... 详情透视!约局吧德州真的有透视挂吗,微乐河南麻将辅助ios,机巧教程(真的有挂)-哔哩哔哩1、约局吧...
教你透视!拱趴大菠萝有挂吗,微... 教你透视!拱趴大菠萝有挂吗,微信小程序有挂吗辅助,绝活儿教程(揭秘有挂)-哔哩哔哩拱趴大菠萝有挂吗透...
普及透视!xpoker辅助怎么... 普及透视!xpoker辅助怎么用,微乐小程序免费脚本,办法教程(有挂攻略)-哔哩哔哩1、打开软件启动...