Ejemplo n.º 1
0
        public BooleanNotifier2ViewModel()
        {
            var booleanNotifier  = new BooleanNotifier(initialValue: false);
            var reactiveProperty = new ReactiveProperty <bool>(initialValue: false).AddTo(CompositeDisposable);

            BooleanNotifierButtonCommand0 = booleanNotifier.ToReactiveCommand().AddTo(CompositeDisposable);
            ReactivePropertyButtonCommand = reactiveProperty.ToReactiveCommand().AddTo(CompositeDisposable);

            // CanExecute method of ReactiveCommand returns true as default.
            // So, you set initialValue explicitly to `booleanNotifier.Value`.
            BooleanNotifierButtonCommand1 = booleanNotifier
                                            .ToReactiveCommand(initialValue: booleanNotifier.Value)
                                            .AddTo(CompositeDisposable);

            // Or if you would like to convert to something using Select and others
            // before calling ToReactiveCommand, you can use StartWith.
            BooleanNotifierButtonCommand2 = booleanNotifier
                                            .StartWith(booleanNotifier.Value)
                                            .Select(x => Something(x))
                                            .ToReactiveCommand()
                                            .AddTo(CompositeDisposable);
        public TimerStartCommandViewModel()
        {
            // ctorからカウント開始
            var ctorDateTime = DateTime.Now;

            MyTimer1 = Observable.Interval(TimeSpan.FromSeconds(1))
                       .Select(_ => DateTime.Now - ctorDateTime)
                       .ToReadOnlyReactivePropertySlim()
                       .AddTo(CompositeDisposable);

#if false
            // 1. DataTime.Now からの差分でカウント  ◆もう少しかっちょよい実装ない?
            var clickDateTime = DateTime.MinValue;
            MyTimer2 = Observable.Interval(TimeSpan.FromSeconds(1))
                       .Where(_ => clickDateTime != DateTime.MinValue)
                       .Select(_ => DateTime.Now - clickDateTime)
                       .ToReadOnlyReactivePropertySlim()
                       .AddTo(CompositeDisposable);

            StartCommand = new ReactiveCommand()
                           .WithSubscribe(() => clickDateTime = DateTime.Now, CompositeDisposable.Add);

            StopCommand = new ReactiveCommand()
                          .WithSubscribe(() => clickDateTime = DateTime.MinValue, CompositeDisposable.Add);
#else
            // 2. BooleanNotifier で管理。Command.CanExecute も取れるので良い感じ
            var timerRunning = new BooleanNotifier();       // BooleanNotifier は IDisposable じゃないので Good!
            //var timerRunning = new ReactivePropertySlim<bool>(initialValue: false).AddTo(CompositeDisposable);

            MyTimer2 = Observable.Interval(TimeSpan.FromSeconds(1))
                       .TakeWhile(_ => timerRunning.Value)
                       .Repeat()
                       .Select(sec => TimeSpan.FromSeconds(sec))
                       .ToReadOnlyReactivePropertySlim()
                       .AddTo(CompositeDisposable);

            StartCommand = timerRunning.Inverse()
                           .ToReactiveCommand(!timerRunning.Value)
                           .WithSubscribe(() => timerRunning.TurnOn(), CompositeDisposable.Add);

            // ◆BooleanNotifier は (Rpと違って) Subscribe 時に LatestValue を発行しないので初期値の指定が必要する
            StopCommand = timerRunning
                          .ToReactiveCommand(timerRunning.Value)
                          .WithSubscribe(() => timerRunning.TurnOff(), CompositeDisposable.Add);
#endif
        }