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
}

相关内容

热门资讯

玩家必看科普!wepokerh... 玩家必看科普!wepokerh5破解,wepoker透视破解版,解密教程(有挂技巧);玩家必备必赢加...
盘点一款!德州局透视脚本免费版... 盘点一款!德州局透视脚本免费版下载手机版,wepoker私人局怎么玩,可靠教程(有挂神器);《WPK...
一分钟了解!pokemmo手机... 一分钟了解!pokemmo手机版修改器,wejoker辅助软件视频,黑科技教程(有挂透视);玩家必备...
实测分享!wpk透视辅助下载,... 1、实测分享!wpk透视辅助下载,wepoker辅助器如何使用,攻略教程(有挂攻略);详细教程。2、...
玩家必看科普!epoker透视... 玩家必看科普!epoker透视,wepoker黑侠辅助器,透明挂教程(有挂软件)1、很好的工具软件,...
总算清楚!aapoker安装包... 总算清楚!aapoker安装包怎么使用,智星菠萝可以辅助吗,总结教程(有挂教程)是一款可以让一直输的...
避坑细节!pokemmo辅助脚... 避坑细节!pokemmo辅助脚本,wepokerplus开挂,教你攻略(有挂技巧);支持多人共享记分...
玩家科普!wejoker辅助器... 玩家科普!wejoker辅助器怎么卖,hhpoker是正品吗,必备教程(有挂方法);原来确实真的有挂...
最新技巧!hhpoker德州透... 最新技巧!hhpoker德州透视挂,wejoker辅助机器人,黑科技教程(有挂技巧);人气非常高,a...
盘点几款!wepoker透视脚... 盘点几款!wepoker透视脚本是什么,wepoker有插件吗,细节揭秘(有挂透视);建议优先通过w...