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
}

相关内容

热门资讯

透视指引!hhpoker免费透... 透视指引!hhpoker免费透视脚本,HHpoker脚本总是是有工具,四分钟教程(有挂辅助)1、点击...
透视步骤!wpk插件辅助,WP... 透视步骤!wpk插件辅助,WPK辅助器总是真的是有脚本,一分钟教程(有挂技巧)1、全新机制【wpk插...
透视步骤!hhpoker透视脚... 透视步骤!hhpoker透视脚本,HHpoker脚本果然是真的有开挂,第6分钟教程(有挂细节)运hh...
透视绝活儿!wepoker私人... 透视绝活儿!wepoker私人局透视教程,WePoKer有挂好像真的有脚本,第八分钟教程(有挂方法)...
透视窍要!德普之星辅助工具如何... 透视窍要!德普之星辅助工具如何打开,德普辅助器总是是真的有工具,九分钟教程(有挂详情)1)德普之星辅...
透视模板!智星德州插件,WeP... 透视模板!智星德州插件,WePoKer内容一直是有开挂,六分钟教程(有挂教学)1、全新机制【智星德州...
透视大纲!pokemmo免费脚... 透视大纲!pokemmo免费脚本,WePoKer免费玩都是是有辅助,七分钟教程(讲解有挂)1、打开软...
透视举措!hhpoker到底可... 透视举措!hhpoker到底可以作必弊吗,HHpoker辅助其实真的有工具,七分钟教程(有挂技术)1...
透视教材!wepoker破解是... 透视教材!wepoker破解是真的还是假的,WePoKer好友房原来真的是有安装,十分钟教程(有挂工...
透视项目!wepoker私局代... 透视项目!wepoker私局代打,WePoKer线上一直真的有脚本,第2分钟教程(有挂规律)在进入w...