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透视app下载,教程妙计(果真有挂)-哔...
透视解迷!pokemmo辅助器... 透视解迷!pokemmo辅助器(透视)wepoker手机版辅助,教程绝活(有挂工具)-哔哩哔哩wep...
透视解迷!wepoker免费透... 透视解迷!wepoker免费透视(透视)wpk透视是真的吗,教程方针(有挂技巧)-哔哩哔哩1、全新机...
透视总结!hh poker透视... 透视总结!hh poker透视器下载(透视)fishpoker透视,教程演示(有挂神器)-哔哩哔哩1...
透视必备!wepoker祈福有... 透视必备!wepoker祈福有用吗(透视)wpk模拟器,教程方针(有挂神器)-哔哩哔哩1)wepok...
透视有挂!wepoker如何设... 透视有挂!wepoker如何设置透视(透视)wepoker可以开透视吗,教程讲义(有挂技术)-哔哩哔...
透视总结!wepoker免费辅... 透视总结!wepoker免费辅助器(透视)德普之星辅助工具如何打开,教程要领(有挂猫腻)-哔哩哔哩小...
透视解密!wepoker如何设... 透视解密!wepoker如何设置透视(透视)佛手在线大菠萝为什么都输,教程指南书(有人有挂)-哔哩哔...
透视开挂!wepoker有透视... 透视开挂!wepoker有透视底牌吗(透视)pokemmo脚本手机版,教程法子(有挂存在)-哔哩哔哩...
透视解密!wepoker有插件... 透视解密!wepoker有插件吗(透视)hhpoker破解工具,教程法门(有挂教学)-哔哩哔哩;1、...