1
我想實現一個視圖/視圖模型,當應用程序檢查用戶是否通過身份驗證時將顯示該視圖/視圖模型。加載ReactiveUI執行命令
下面的代碼以多次調用路由器結束。 InternalIsAuthenticated中的測試代碼有問題嗎?我怎樣才能實現這個正確的?
視圖模型:
public class LoadingViewModel : ReactiveObject, IRoutableViewModel
{
public string UrlPathSegment {
get {
return "Loading";
}
}
public IScreen HostScreen { get; protected set; }
public ReactiveCommand<bool> IsAuthenticatedCommand { get; protected set; }
public LoadingViewModel (IScreen screen)
{
HostScreen = screen;
IsAuthenticatedCommand = ReactiveCommand.CreateAsyncTask (async (_, ctx) => await InternalIsAuthenticated());
IsAuthenticatedCommand.Subscribe(next => {
//called twice. ??
HostScreen.Router.NavigateAndReset.Execute(new OnboardingViewModel(HostScreen));
});
IsAuthenticatedCommand.ThrownExceptions.Subscribe(ex => {
Debug.WriteLine(ex.Message);
UserError.Throw("oops");
});
//Error: nested push animation can result in corrupted navigation bar
//Error: Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
//this.WhenAnyValue(x => x.UrlPathSegment)
// .Throttle(TimeSpan.FromSeconds(1), RxApp.MainThreadScheduler)
// .InvokeCommand(this, x => x.IsAuthenticatedCommand);
//Error: nested push animation can result in corrupted navigation bar
//Error: Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
this.IsAuthenticatedCommand.ExecuteAsyncTask();
//OnboardingView ends on top of LoadingView in the navigation stack.
//HostScreen.Router.NavigateAndReset.Execute(new OnboardingViewModel(HostScreen));
}
private async Task<bool> InternalIsAuthenticated(){
return await Task.Run(() => {return true;});
}
}
查看:
public partial class LoadingView : ContentPage, IViewFor<LoadingViewModel>
{
public LoadingView()
{
InitializeComponent();
this.WhenAnyValue (x => x.ViewModel).BindTo (this, x => x.BindingContext);
}
public LoadingViewModel ViewModel {
get { return (LoadingViewModel)GetValue(ViewModelProperty); }
set { SetValue(ViewModelProperty, value); }
}
public static readonly BindableProperty ViewModelProperty =
BindableProperty.Create<LoadingView, LoadingViewModel>(x => x.ViewModel, default(LoadingViewModel), BindingMode.OneWay);
object IViewFor.ViewModel {
get { return ViewModel; }
set { ViewModel = (LoadingViewModel)value; }
}
}