启用/禁用延迟的

时间:2015-07-06 09:03:57

标签: wpf reactiveui

使用WPF和ReactiveUI进行简单的回合制游戏?我是Reactive UI / Reactive Extensions的新手。

在一个特定的视图中,我有3个按钮,说" Kick"," Punch"," Run Away"。

点击这些按钮时,它会调用Fight Class的Kick,Punch或RunAway功能,所有这些功能都会返回一个字符串,我会在视图中显示。

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

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

我想做以下事情。

当命令被触发时,我想要禁用所有命令,持续时间为2秒,同时显示消息,然后在两秒后清除消息并再次启用命令。

提前致谢。

1 个答案:

答案 0 :(得分:1)

这是一种方法,用评论:

        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);

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