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透视最简单三个步骤,aapoker透视脚本入口,妙计教程(有挂秘籍)1、许...
四分钟辅助!佛手在线大菠萝辅助... 四分钟辅助!佛手在线大菠萝辅助,wepoker有透视底牌吗,演示教程(有挂规律)1、金币登录送、破产...
六分钟辅助!德普之星透视,po... 六分钟辅助!德普之星透视,pokerworld修改器,课程教程(竟然有挂)1、不需要AI权限,帮助你...
七分钟辅助!wepoker私人... 七分钟辅助!wepoker私人局辅助器怎么用,hh poker辅助器先试用,总结教程(有挂分享)1、...
一分钟辅助!wepoker插件... 一分钟辅助!wepoker插件辅助,wepoker有透视吗,课程教程(有挂技巧)1、wepoker插...
5分钟辅助!xpoker辅助助... 5分钟辅助!xpoker辅助助手,wepoker游戏的安装教程,办法教程(真的有挂)1、上手简单,内...
第一分钟辅助!steampok... 第一分钟辅助!steampokermaster辅助,hhpoker透视脚本视频,秘籍教程(有挂分享)...
第五分钟辅助!wepoker辅... 第五分钟辅助!wepoker辅助器官方,wepoker辅助器安装包定制,总结教程(有挂透明挂)1、进...
六分钟辅助!wpk透视辅助靠谱... 六分钟辅助!wpk透视辅助靠谱吗,线上德州的辅助器是什么,机巧教程(真的有挂)1、操作简单,无需线上...
9分钟辅助!wepoker私人... 9分钟辅助!wepoker私人局辅助器怎么用,德州透视脚本,指南教程(有挂透视)1、首先打开wepo...