Exemplo n.º 1
0
        public static void Run()
        {
            #region Example
            var example = new Example();

            if (Console.ReadKey().Key == ConsoleKey.OemMinus)
            {
                var counter = new Counter2();
                example.Start(counter.Run);
            }
            else if (Console.ReadKey().Key == ConsoleKey.D5)
            {
                var counter = new NeCounter();
                example.Start(counter.Run);
                Console.WriteLine(example.StartNotVoid(counter.Return));
            }
            else
            {
                var counter = new Counter1();
                example.Start(counter.Run);
            }

            NeCounter.AAA aaa = new NeCounter.AAA();

            //Example.ExampleDelegate delegateReference = NeCounter.RunStatic;

            //example.Start(delegateReference);

            #endregion

            #region Example1

            var neCounter = new NeCounter();

            var example2 = new ExampleActionAndFunc();

            Action <int, string> action1 = (count, str) =>
            {
                Console.WriteLine(str);

                for (int i = count; i >= 0; i--)
                {
                    Console.WriteLine("Пять");
                }
            };

            example2.Start(action1);

            example2.Start(a => Console.WriteLine(a));
            #endregion

            var list = new List <string>();
            list.Add("adasd");
            var filtered = list.Where((str, index) => str[0] == 'a');
            //var filtered = list.Where((str, index) =>
            //{
            //    Console.WriteLine(index);
            //    return str[0] == 'a';
            //});
        }
        }                                         // コンストラクタで生成するので初期化不要

        public ReactiveCommand1ViewModel()
        {
            // 宣言時にインスタンスを作ってて、コンストラクタで Subscribe()
            Command1
            .Subscribe(() => Counter1.Increment())
            .AddTo(CompositeDisposable);

            // View の CommandParameter を加算。 WithSubscribeにより宣言からDispose登録まで一気通貫。
            Command2 = new ReactiveCommand <int>()
                       .WithSubscribe(x => Counter2.Increment(x), CompositeDisposable.Add);

            // CheckBox により(IObservable<bool>)から ReactiveCommand を作成
            Command31 = CheckFlag31.ToReactiveCommand(initialValue: false)
                        .WithSubscribe(() => Counter3.Increment(), CompositeDisposable.Add);

            var updateTimeTrigger = new Subject <Unit>();

            CompositeDisposable.Add(updateTimeTrigger);

            // IObservable<bool> から ReactiveCommand を作成
            // 実行後に一定時間は CanExecute を無効にする (ひねくれずに AsyncReactiveCommand を使えばよいと思う)
            Command32 = Observable.Merge(
                updateTimeTrigger.Select(_ => false),
                updateTimeTrigger.Delay(TimeSpan.FromSeconds(0.5)).Select(_ => true))
                        .ToReactiveCommand()
                        .AddTo(CompositeDisposable);

            Command32
            .Do(_ => updateTimeTrigger.OnNext(Unit.Default))
            .Subscribe(_ => Counter3.Increment())
            .AddTo(CompositeDisposable);
        }