2014-12-24 74 views
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; } 
     } 
    } 

回答

2

如果NavigateAndReset获取调用两次,也许是因为你创建两个LoadingViewModels?