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有插件吗(辅助挂)果然真的是有挂(有挂猫腻辅助插件)1、完成辅助器v3.3...
每日必看教程!aapoker辅... 每日必看教程!aapoker辅助工具安全吗(辅助挂)本来真的是有挂(有挂秘诀辅助工具)1、透视辅助软...
揭秘!xpoker辅助器(辅助... 揭秘!xpoker辅助器(辅助挂)都是是真的有挂(有挂分享辅助脚本)运辅助工具,进入游戏界面。进入辅...
实测交流!德普之星辅助软件(辅... 实测交流!德普之星辅助软件(辅助挂)本来是真的有挂(详细教程辅助教程)1、许多玩家不知道辅助怎么退出...
重大消息!wpk软件是正规的吗... 重大消息!wpk软件是正规的吗(辅助挂)果然是真的有挂(有挂技巧辅助教程)1、这是跨平台的轻量版有透...
科技新动态!pokemmo脚本... 科技新动态!pokemmo脚本辅助器下载(辅助挂)一直是真的有挂(有挂总结辅助方法)1、点击下载安装...
分享个大家!wepoker黑侠... 分享个大家!wepoker黑侠破解(辅助挂)果然真的是有挂(揭秘有挂辅助方法)能透视中分为三种模型:...
一分钟快速了解!xpoker辅... 一分钟快速了解!xpoker辅助工具(辅助挂)真是存在有挂(有挂教学辅助app)运辅助工具,进入游戏...
玩家必备科普!uupoker有... 玩家必备科普!uupoker有透视吗(辅助挂)都是是真的有挂(有挂秘籍辅助方法)1、金币登录送、破产...
一分钟了解!wepoker透视... 一分钟了解!wepoker透视脚本免费下载(辅助挂)都是真的有挂(发现有挂辅助教程)亲,关键说明,透...