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是不是有人用挂,其实真的是有挂(证实有挂)1、让任何用...
分享实测!wpk刷入池率脚本,... 分享实测!wpk刷入池率脚本,wepoker破解器,本来真的有挂(有挂教学)1、操作简单,无需手机版...
我来教教大家!德州辅助工具到底... 我来教教大家!德州辅助工具到底怎么样,智星德州辅助译码插件靠谱吗,总是是真的有挂(有挂分析)1、这是...
热点推荐!wpk官网下载链接,... 热点推荐!wpk官网下载链接,aa poker辅助包,切实是有挂(有挂技术)1、脚本辅助下载、免费透...
揭秘!wepoker正确养号方... 揭秘!wepoker正确养号方法,hhpoker透视脚本下载,一贯真的有挂(有挂教程)1、进入游戏-...
一分钟快速了解!wepoker... 一分钟快速了解!wepoker透视挂底牌,wepoker有辅助吗,其实真的是有挂(有挂助手)1、玩家...
重大通报!wepoker有什么... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
重磅来袭!wpk显示有作弊,约... 重磅来袭!wpk显示有作弊,约局吧是否有挂,一直是有挂(有挂头条)小薇(辅助器软件下载)致您一封信;...
六分钟了解!hhpoker透视... 六分钟了解!hhpoker透视工具,佛手大菠萝辅助,确实真的有挂(有挂神器)1、用户打开应用后不用登...