ReactiveUI在加载时执行命令

时间:2014-12-24 02:19:57

标签: c# mvvm xamarin reactiveui

我正在尝试实现一个view / viewmodel,它将在应用程序检查用户是否经过身份验证时显示。

以下代码最终会多次调用路由器。 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; }
        }
    }

1 个答案:

答案 0 :(得分:2)

如果两次调用NavigateAndReset,可能是因为你要创建两个LoadingViewModels?