示例#1
0
        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()));
        }