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
}

相关内容

热门资讯

WPK有透视指南!aapoke... WPK有透视指南!aapoker辅助插件工具,德州微扑克其实存在有透视挂攻略1、进入到aapoker...
WPK安卓学习!wpk辅助工具... WPK安卓学习!wpk辅助工具,德州微扑克真是真的有辅助攻略WPK安卓学习!wpk辅助工具,德州微扑...
WPK作弊学习!wejoker... 您好,wejoker辅助脚本这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】...
WPK有透视指南!黑侠破解we... WPK有透视指南!黑侠破解wepoker,德州微扑克真是存在有透视挂攻略1、点击下载安装,黑侠破解w...
WPK靠谱经验!wepoker... WPK靠谱经验!wepoker开脚本视频,德州微扑克果然真的有透视攻略1、不需要AI权限,帮助你快速...
WPK脚本攻略!wepoker... WPK脚本攻略!wepoker有辅助工具吗,德州微扑克切实存在有开挂技巧wepoker有辅助工具吗透...
WPK靠谱学习!wepoker... WPK靠谱学习!wepoker脚本下载,德州微扑克其实有透视挂工具在进入wepoker脚本下载软件靠...
WPK官网绝活!wepoker... WPK官网绝活!wepoker作弊辅助,德州微扑克本来是有开挂appWPK官网绝活!wepoker作...
WPK俱乐部模板!wepoke... WPK俱乐部模板!wepoker透视脚本是什么,德州微扑克其实真的是有透视技巧1、wepoker透视...
WPK透视版模板!wepoke... WPK透视版模板!wepoker买脚本靠谱吗,德州微扑克切实真的有辅助器插件1、点击下载安装,wep...