1
我想爲WinForms使用ReactiveUI 6.5。如何使ReactiveCommand同步?
我需要在viewmodel上調用並在視圖上同步執行的命令。我試過ReactiveCommand.Create()
和.Execute()
,但這顯然是異步執行的,這不是我所需要的。
什麼我已經是這樣的:
public class MyViewModel : ReactiveObject
{
public ReactiveCommand<object> AddNewThingToControl { get; private set; }
public ThingType Thing { get; set; }
// Ctor
public MyViewModel()
{
AddNewThingToControl = ReactiveCommand.Create();
}
// Thing factory, called from somwhere not shown
public void CreateAndAddThing()
{
Thing = new ThingType();
// should not return before Thing is added to myControl
AddNewThingToControl.Execute(null);
// Code that needs Thing added to myControl goes here.
// This doesn't work because DoAddThing() has not been called yet!
}
}
public class MyView : Form, IViewFor<MyViewModel>
{
public MyViewModel VM { get; set; }
// Ctor
public MyView()
{
VM = new MyViewModel();
VM.AddNewThingToControl.Subscribe(x => { DoAddThing(); });
}
private void DoAddThing()
{
// This doesn't get called until some unknown time
// after AddNewThingToControl.Execute() returns!
myControl.Things.Add(VM.Thing);
}
// -----------
#region IViewFor Implementation not shown
}
CreateAndAddThing()
由代碼調用無論是在UI線程或者從某種工作線程。
創建了一個新的Thing
,應該在到達AddNewThingToControl.Execute()
之後的行之前將其添加到視圖上的控件。
我該怎麼做ReactiveCommand
?