Enable/Disable 有延迟的反应命令

Enable/Disable Reactive Commands with Delay

我正在使用 WPF 和 ReactiveUI 开发简单的回合制游戏吗?我对 Reactive UI/Reactive Extensions 很陌生。

在特定视图上,我有 3 个按钮,例如 "Kick"、"Punch"、"Run Away"。

单击这些按钮中的任何一个,它将调用 Fight Class 的 Kick、Punch 或 RunAway 函数,所有这些函数 return 一个字符串,我将其显示在视图中.

this.KickCommand = ReactiveCommand.CreateCommand();
this.KickCommand.Subscribe(x => 
    {
        this.Message = this.Fight.Kick();
    });

同样,我还有剩余的命令。

我想做以下事情。

触发命令时,我希望在显示消息时禁用所有命令 2 秒,然后在两秒后清除消息并再次启用命令。

提前致谢。

这是一种方法,附有评论:

        var canExecute = new Subject<bool>();
        KickCommand = ReactiveCommand.Create(canExecute);
        PunchCommand = ReactiveCommand.Create(canExecute);
        RunAwayCommand = ReactiveCommand.Create(canExecute);
        new[] { KickCommand, PunchCommand, RunAwayCommand }.Select(cmd => {
            // skip the initial false, we don't want to delay that one
            var isExec = cmd.IsExecuting.Skip(1);
            // delay re-activation (falses) by 2s
            return new[] { isExec.Where(x => x), isExec.Where(x => !x).Delay(TimeSpan.FromSeconds(2)) }.Merge()
            // add back an initial false
            .StartWith(false);
        })
            // all commands needs to be in this non-executing-since-2s state
            .CombineLatest(l => l.All(x => !x))
            .ObserveOn(RxApp.MainThreadScheduler)
            .Do(ClearMessageIfTrue)
            .Subscribe(canExecute);

我跳过了命令订阅部分,因为你似乎已经有了那个部分。