我试图围绕reactiveUI,最近从6.5.2更新到7.0,似乎包括一些关于ReactiveCommand的重大变化。ReactiveUI 7.0,ReactiveCommand,订阅永远不会触发?
EG这个曾经工作:
在视图模型:
public ReactiveCommand<Unit> DoLogin;
...
DoLogin = ReactiveCommand.CreateAsyncTask(
async x => {
IsBusy = true;
await Task.Delay(2500);
IsBusy = false;
return Unit.Default;
});
在查看:
//bind the command
Observable.FromEventPattern(x => loginButton.Clicked += x, x => loginButton.Clicked -= x)
.Subscribe(args => ViewModel.DoLogin.Execute(null));
//do something after the dologin is complete
this.WhenAnyObservable(x => x.ViewModel.DoLogin)
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(x => {
DisplayAlert("login complete", "welcome", "OK");
}
);
但是现在reactiveui 7.0,它是不同的,我不得不做出一些更改,我无法正常工作:
in ViewModel:
public ReactiveCommand<Unit, Unit> DoLogin;
...
DoLogin = ReactiveCommand.CreateFromTask(
async x => {
IsBusy = true;
await Task.Delay(2500);
IsBusy = false;
return Unit.Default;
});
在查看:
//bind the command
Observable.FromEventPattern(x => loginButton.Clicked += x, x => loginButton.Clicked -= x)
.Subscribe(args => ViewModel.DoLogin.Execute());
//do something after the dologin is complete
this.WhenAnyObservable(x => x.ViewModel.DoLogin)
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(x => {
DisplayAlert("login complete", "welcome", "OK");
}
);
命令代码仍然被执行,但WhenANyObservable订阅部分永远不会触发。它从不显示我的DisplayAlert。
我正在使用Xamarin Forms,如果这很重要,但即使在Windows窗体中我也会得到相同的行为。
是的,伟大的。我设法解决了这个问题。现在它变得更清洁了。在尝试学习某些我不明白的东西时遇到了困难,同时语法也发生了变化! – 101chris