예제 #1
0
        public NewProjectViewModel(IScreen hostScreen) : base("newprj", hostScreen)
        {
            var configureAppModel = new ConfigureAppViewModel(this);

            Router.Navigate.Execute(configureAppModel);

            OpenSetupWizard = ReactiveCommand.CreateFromObservable <Unit, Unit>(sender =>
            {
                return(ShowSetupWizard.Handle(Unit.Default));
            });

            IObservable <WizardPageViewModel> pageObservable = Router.CurrentViewModel
                                                               .Where(vm => vm is WizardPageViewModel)
                                                               .Select(vm => vm as WizardPageViewModel);

            pageObservable.Select(p => p.Title).BindTo(this, t => t.Title);
            pageObservable.Select(p => p.Description).BindTo(this, t => t.Description);
            pageObservable.Select(p => p.IsDescriptionVisible).BindTo(this, t => t.IsDescriptionVisible);
            pageObservable.Subscribe(p => p.WhenAnyValue(page => page.IsBusy).BindTo(this, t => t.IsBusy));

            pageObservable.Select(p => p.NextText).ToProperty(this, t => t.NextText, out _nextText);
            pageObservable.Select(p => p.BackText).ToProperty(this, t => t.BackText, out _backText);
            pageObservable.Select(p => p.BackVisible).ToProperty(this, t => t.HasBack, out _hasBack);

            pageObservable
            .Select(p =>
            {
                ReactiveCommand <Unit, Unit> cmdDoSomething = ReactiveCommand.CreateFromTask(p.OnNext, outputScheduler: RxApp.MainThreadScheduler);

                ReactiveCommand <Unit, Unit> cmdNavigateOrClose;
                if (p.IsFinishPage)
                {
                    cmdNavigateOrClose = ReactiveCommand.CreateFromTask(async() =>
                    {
                        await cmdDoSomething.Execute();
                        await Router.NavigateAndReset.Execute(new ConfigureAppViewModel(this));
                    }, canExecute: p.WhenAnyValue(page => page.NextEnabled), outputScheduler: RxApp.MainThreadScheduler);
                }
                else
                {
                    cmdNavigateOrClose = ReactiveCommand.CreateFromTask(async() =>
                    {
                        await cmdDoSomething.Execute();
                        await Router.Navigate.Execute(p.GetNextPage());
                    }, canExecute: p.WhenAnyValue(page => page.NextEnabled), outputScheduler: RxApp.MainThreadScheduler);
                }

                cmdNavigateOrClose
                .ThrownExceptions
                //.FirstAsync()
                .Throttle(TimeSpan.FromMilliseconds(250), RxApp.MainThreadScheduler)
                .Subscribe(async ex =>
                {
                    await ShowError.Handle(ex);
                });

                return(cmdNavigateOrClose);
            })
            .ToProperty(this, t => t.GoNext, out _goNext);

            this.WhenActivated(disposables =>
            {
                if (Router.NavigationStack.Count > 0)
                {
                    IRoutableViewModel fistPage = Router.NavigationStack[0];
                    Router.NavigateAndReset.Execute(fistPage);
                    Router.NavigateAndReset.Execute(fistPage).Subscribe().DisposeWith(disposables);
                }
                //else
                //{
                //    Router.Navigate.Execute(new ConfigureAppViewModel(this)).Subscribe().DisposeWith(disposables);
                //}
            });
        }