我正在使用Win40的Net40 ReactiveUI,当我有这个时它可以正常工作:
public class VMUpdates : ReactiveObject
{
...
public ReactiveCommand<object> btnClick { get; private set; }
public VMUpdates(frmMain frmMain)
{
...
btnClick = ReactiveCommand.Create();
btnClick.Subscribe(_ => ExecutableMethod());
}
private void ExecutableMethod()
{
...
}
}
和此:
public partial class FrmUpdates : Form, IViewFor<VMUpdates>
{
...
public FrmUpdates(frmMain)
{
ViewModel = new VMUpdates(FrmMain);//Initialise VM
InitializeComponent();
...
this.BindCommand(ViewModel, vm => vm.btnClick, view => view.btn);
}
public VMUpdates ViewModel { get; set; }
object IViewFor.ViewModel
{
get { return ViewModel; }
set { ViewModel = (VMUpdates)value; }
}
但是只要我将ReactiveCommand添加到完全不同的VM,ExecutableMethod方法就不再执行了。
public class VMStatus : ReactiveObject
{
public ReactiveCommand<object> btn2Click { get; private set; }
public VMStatus()
{
btn2Click = ReactiveCommand.Create();
btn2Click.Subscribe(_ => SomeMethod());
}
private void SomeMethod()
{
...
}
}
加
public partial class frmStatus : Form, IViewFor<VMStatus>
{
public frmStatus()
{
ViewModel = new VMStatus();//Initialise VM
InitializeComponent();
...
this.BindCommand(ViewModel, vm => vm.btn2Click, view => view.button2);
}
public VMStatus ViewModel { get; set; }
object IViewFor.ViewModel
{
get { return ViewModel; }
set { ViewModel = (VMStatus)value; }
}
}
答案 0 :(得分:1)
编辑:当我将语法改为此时,它有效:
public ReactiveCommand<Unit> btnClick { get; private set; }
public VMUpdates(frmMain frmMain)
{
...
btnClick = ReactiveCommand.CreateAsyncObservable(x => ExecutableMethod());
}
public IObservable<Unit> ExecutableMethod()
{
return Observable.Start(() => {
...
});
}