2017-08-03 71 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

謝謝,你是對的。 –