Esempio n. 1
0
        public static void Run()
        {
            _container = new UnityContainer();
            _container.RegisterType <IResolver, UnityResolver>(new ContainerControlledLifetimeManager());
            _vm = _container.Resolve <ViewModel>();
            _vm.Items.CollectionChanged += (s, e) =>
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine($"Collection Changed: {e.AsString()}");
            };

            MvvmRx.ObserveMany(_vm, _vm.Items, item => item.DoMath, (item, number) => $"item: {item.Uid}, number: {number}")
            .Subscribe(val =>
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(val);
            }).DisposedBy(_vm);

            Console.WriteLine(_model.ToJson());

            _subj = new BehaviorSubject <ImmutableList <ItemModel> >(ImmutableList <ItemModel> .Empty);
            _vm.Initialize(_subj);

            _subj.ObserveDiff(m => m.Uid)
            .Subscribe(diff =>
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(diff.ToJson());
            }).DisposedBy(_vm);

            _do(ImmutableList.Create(
                    new ItemModel("1", "First", "Odd"),
                    new ItemModel("2", "Second", "Even"),
                    new ItemModel("3", "Third", "Odd"),
                    new ItemModel("4", "Fourth", "Even"),
                    new ItemModel("5", "Fifth", "Odd")));

            var cmd3 = _vm.Items[2].DoMath;

            // 1, 2, 5, 4
            _do(_model
                .RemoveAt(2)
                .Move(2, 3));

            _vm.Items[1].DoMath.Execute(100);
            _vm.Items.Last().DoMath.Execute(200);

            // this should not do anything becuase the command is not in the loop anymore
            // since the owner of the command is disposed, this shoud throw an error
            try
            {
                cmd3.Execute(300);
            }
            catch (ObjectDisposedException ex)
            {
                Console.WriteLine("Thrown object disposed exception as expected");
            }


            // 6, 4
            _do(_model
                .SetItem(1, new ItemModel("6", "Sixth", "Even"))
                .Add(new ItemModel("7", "Seventh", "Odd"))
                .RemoveAll(m => m.Category == "Odd"));

            // 6*, 4
            _do(_model
                .SetItem(0, _model[0].With(x => x.DisplayName, "New Sixth").With(x => x.Category, "Edited"))
                .RemoveAt(1)
                .Insert(0, new ItemModel("4", "New Fourth", "Edited")));

            _vm.AddItem.Subscribe(x =>
            {
                _do(_model
                    .Add(new ItemModel((++_counter).ToString(), "Item " + _counter, "Category " + _counter)));
            },
                                  () => Console.WriteLine("Command Completed")
                                  );

            _vm.RemoveItem.Subscribe(id =>
            {
                _do(_model.RemoveAll(x => x.Uid == id));
            });


            _vm.AddItem.Execute(null);
            _vm.AddItem.Execute(null);
            _vm.RemoveItem.Execute("4");

            MvvmRx.ObservePropertyChanges(_vm, x => x.Caption)
            .Subscribe(x => Console.WriteLine($"Property Caption changed from {x.oldValue} to {x.newValue}"),
                       () => Console.WriteLine("Property observer completed"));

            // property changes observable should fire two events
            _vm.Caption = "A";
            _vm.Caption = "B";



            _vm.Dispose();
        }