void setupRx() { var countAsBehavior = Observable.Concat( Observable.Defer(() => Observable.Return(_NavigationStack.Count)), NavigationStack.CountChanged); NavigateBack = ReactiveCommand.CreateAsyncObservable( countAsBehavior.Select(x => x > 1), _ => Observable.Return(Unit.Default)); NavigateBack.Subscribe(_ => NavigationStack.RemoveAt(NavigationStack.Count - 1)); Navigate = new ReactiveCommand <object>(Observable.Return(true), x => Observable.Return(x)); Navigate.Subscribe(x => { var vm = x as IRoutableViewModel; if (vm == null) { throw new Exception("Navigate must be called on an IRoutableViewModel"); } NavigationStack.Add(vm); }); NavigateAndReset = new ReactiveCommand <object>(Observable.Return(true), x => Observable.Return(x)); NavigateAndReset .SelectMany(x => { NavigationStack.Clear(); return(Navigate.ExecuteAsync(x)); }).Subscribe(); CurrentViewModel = Observable.Concat( Observable.Defer(() => Observable.Return(NavigationStack.LastOrDefault())), NavigationStack.Changed.Select(_ => NavigationStack.LastOrDefault())); }
/// <summary> /// Initializes a new instance of the <see cref="NavigationViewModel"/> class. /// </summary> public NavigationViewModel() { history = new ReactiveList <IPanePageViewModel>(); history.Changing.Subscribe(CollectionChanging); history.Changed.Subscribe(CollectionChanged); var pos = this.WhenAnyValue( x => x.Index, x => x.History.Count, (i, c) => new { Index = i, Count = c }); content = pos .Where(x => x.Index < x.Count) .Select(x => x.Index != -1 ? history[x.Index] : null) .StartWith((IPanePageViewModel)null) .ToProperty(this, x => x.Content); this.WhenAnyValue(x => x.Content) .Buffer(2, 1) .Subscribe(x => { if (x[0] != null && history.Contains(x[0])) { x[0].Deactivated(); } x[1]?.Activated(); }); NavigateBack = ReactiveCommand.Create(() => { }, pos.Select(x => x.Index > 0)); NavigateBack.Subscribe(_ => Back()); NavigateForward = ReactiveCommand.Create(() => { }, pos.Select(x => x.Index < x.Count - 1)); NavigateForward.Subscribe(_ => Forward()); }
/// <summary> /// Initializes a new instance of the <see cref="NavigationViewModel"/> class. /// </summary> public NavigationViewModel() { history = new ReactiveList <IPanePageViewModel>(); history.BeforeItemsAdded.Subscribe(BeforeItemAdded); history.CollectionChanged += CollectionChanged; var pos = this.WhenAnyValue( x => x.Index, x => x.History.Count, (i, c) => new { Index = i, Count = c }); content = pos .Where(x => x.Index < x.Count) .Select(x => x.Index != -1 ? history[x.Index] : null) .StartWith((IPanePageViewModel)null) .ToProperty(this, x => x.Content); NavigateBack = ReactiveCommand.Create(pos.Select(x => x.Index > 0)); NavigateBack.Subscribe(_ => Back()); NavigateForward = ReactiveCommand.Create(pos.Select(x => x.Index < x.Count - 1)); NavigateForward.Subscribe(_ => Forward()); }