2017-08-03 72 views
0

我试图找到一种方法,命令的生命周期内改变ReactiveUI ReactiveCommand的谓词: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; } 
} 

当从绑定到这个视图模型的视图下执行命令,它总是给“初始一步“的信息而不是”另一个步骤“。 任何想法,将不胜感激。

回答

0

您将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; } 
} 
+0

谢谢,你是对的。 –