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透视脚本是什么,透明教程(确实真的有挂);wep...
透视有挂!wepoker底牌透... 透视有挂!wepoker底牌透视脚本怎样安装,we-poker辅助,必备教程(都是真的有挂)科技教程...
透视安装!aapoker辅助工... 透视安装!aapoker辅助工具免费下载,hh poker辅助有用吗,科技教程(原来有挂);科技安装...
透视系统!智星菠萝可以辅助吗,... 透视系统!智星菠萝可以辅助吗,wpk透视辅助方法,可靠教程(原本存在有挂)是一款可以让一直输的玩家,...
透视辅助!wepoker透视脚... 1、透视辅助!wepoker透视脚本安卓,福建大菠萝万能辅助器,科技教程(本来真的是有挂);详细教程...
透视模拟器!wpk辅助器安装,... 透视模拟器!wpk辅助器安装,拱趴大菠萝万能辅助器,透明挂教程(原来真的有挂);wpk辅助器安装是一...
透视了解!wepoker辅助器... 透视了解!wepoker辅助器怎么用,wepoker轻量版书签,我来教教你(一直真的是有挂);亲,其...
透视插件!aapoker透视脚... 透视插件!aapoker透视脚本安装包,wepoker透视版下载,透明挂教程(一向真的有挂)1、不需...
透视黑科技!wepoker免费... 透视黑科技!wepoker免费永久脚本,德州机器人代打脚本,力荐教程(起初是真的有挂)这是由厦门游乐...
透视最新!aapoker辅助器... 自定义aapoker辅助器怎么用系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅...