ReactiveCommand.Execute 未触发 IsExecuting

ReactiveCommand.Execute not triggering IsExecuting

我订阅了命令的 IsExecuting:

LoginCommand.IsExecuting.Subscribe(x => Log("Logging in"));

当我的命令被 InvokeCommand 调用时它工作正常但是当我调用它时:

LoginCommand.Execute();

IsExecuting 可观察对象未被触发。

这个有效:

Observable.Start(() => { }).InvokeCommand(LoginCommand);

有人知道为什么在调用 Execute 方法时 IsExecuting 属性 没有改变吗?我正在尝试对命令进行单元测试,所以我认为这是从测试中执行命令的最佳方式。

升级到ReactiveUI 7.0后,Execute()方法发生了变化。现在它不会立即触发命令。相反,它 returns 冷酷 IObservable 您必须订阅才能让事情发生。

LoginCommand.Execute().Subscribe();

查看 release notes 中有关 RxUI 7.0 更改的文章。 Ctrl+F "ReactiveCommand is Better"。它明确指出:

the Execute exposed by ReactiveCommand is reactive (it returns IObservable). It is therefore lazy and won't do anything unless something subscribes to it.

当你想执行一个ReactiveCommand时,你可以这样做:

RxApp.MainThreadScheduler.Schedule(Unit.Default, (scheduler, state) => 
       ViewModel.MyCommand.Execute(state).Subscribe());

然后您可以像这样订阅它:

this.WhenActivated(d => { 

    MyCommand
        .Select(_ => this)
        .ObserveOn(RxApp.MainThreadScheduler)
        .ExecuteOn(RxApp.TaskScheduler)
        .Subscribe(viewModel => {
            // ... 
        })
        .DisposeWith(d); 
});