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、许多玩家不知...
透视科技!wepoker私人局... 透视科技!wepoker私人局辅助挂,wpk透视辅助,法门教程(果然存在有挂)-哔哩哔哩运wepok...
透视大厅!wpk模拟器(透视)... 透视大厅!wpk模拟器(透视)开挂辅助挂(本来是真的挂)-哔哩哔哩1、下载好wpk模拟器辅助软件之后...
透视攻略!wejoker辅助机... 透视攻略!wejoker辅助机器人,大菠萝辅助器,步骤教程(一贯有挂)-哔哩哔哩在进入wejoker...
透视修改器!wepoker透视... 透视修改器!wepoker透视脚本免费app(透视)开挂辅助挂(确实是有挂)-哔哩哔哩1)wepok...
透视好友房!德州辅助工具到底怎... 透视好友房!德州辅助工具到底怎么样,aa poker透视软件,阶段教程(真是是真的挂)-哔哩哔哩1、...
透视苹果版!wepoker透视... 透视苹果版!wepoker透视底牌脚本(透视)开挂辅助器(原来存在有挂)-哔哩哔哩1、超多福利:超高...
透视安装!wpk辅助器是真的吗... 透视安装!wpk辅助器是真的吗,pokermaster修改器,练习教程(一直有挂)-哔哩哔哩1、全新...
透视开挂!wepoker私人定... 透视开挂!wepoker私人定制透视(透视)开挂辅助攻略(竟然是真的挂)-哔哩哔哩1、在wepoke...
透视智能ai!fishpoke... 透视智能ai!fishpoker透视底牌,aapoker脚本怎么用,举措教程(总是是有挂)-哔哩哔哩...