带有动态谓词的 ReactiveUI ReactiveCommand

ReactiveUI ReactiveCommand with dynamic predicate

我正在尝试找到一种方法来在命令的生命周期内更改 ReactiveUI ReactiveCommand 的谓词:

 public class Form1ViewModel
{
    public Form1ViewModel()
    {
        TestAction = () => Observable.Start(() => { MessageBox.Show("Initial step"); });
        TestCommand = ReactiveCommand.CreateFromObservable(TestAction);
        TestCommand.ThrownExceptions.Subscribe(ex => MessageBox.Show(string.Format("Something went wrong while executing test command: {0}", ex)));
        TestAction = () => Observable.Start(() => { MessageBox.Show("Another step"); });

    }
    public ReactiveCommand TestCommand { get; set; }
    public Func<IObservable<Unit>> TestAction { get; set; }
}

从绑定到此视图模型的视图执行命令时,它总是给出 "Initial step" 消息而不是 "Another step"。 任何想法将不胜感激。

您将 TestAction 的当前值传递给 CreateFromObservable,然后更改了 TestAction 的值。那永远行不通。

你可以试试

 public class Form1ViewModel
{
    public Form1ViewModel()
    {
        TestAction = () => Observable.Start(() => { MessageBox.Show("Initial step"); });
        TestCommand = ReactiveCommand.CreateFromObservable(()=>TestAction());
        TestCommand.ThrownExceptions.Subscribe(ex => MessageBox.Show(string.Format("Something went wrong while executing test command: {0}", ex)));
        TestAction = () => Observable.Start(() => { MessageBox.Show("Another step"); });

    }
    public ReactiveCommand TestCommand { get; set; }
    public Func<IObservable<Unit>> TestAction { get; set; }
}