コード例 #1
0
        public void BindCommandToObjectTargetIsNull()
        {
            var vm   = new CommandBindingViewModel();
            var view = new CommandBindingView {
                ViewModel = vm
            };

            var invokeCount = 0;

            vm.Command2.Subscribe(_ => invokeCount++);

            var sub = new Subject <object>();

            Assert.Throws <Exception>(() =>
            {
                var disp = CreatesCommandBinding.BindCommandToObject(vm.Command2, true, sub);

                view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
                {
                    RoutedEvent = UIElement.MouseUpEvent
                });

                disp.Dispose();

                view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
                {
                    RoutedEvent = UIElement.MouseUpEvent
                });
            });

            Assert.Equal(0, invokeCount);
        }
コード例 #2
0
        public void CommandBindToExplicitEventWireup()
        {
            var vm   = new CommandBindingViewModel();
            var view = new CommandBindingView {
                ViewModel = vm
            };

            var invokeCount = 0;

            vm.Command2.Subscribe(_ => invokeCount++);

            var disp = view.BindCommand(vm, x => x.Command2, x => x.Command2, "MouseUp");

            view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
            {
                RoutedEvent = UIElement.MouseUpEvent
            });

            disp.Dispose();

            view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
            {
                RoutedEvent = UIElement.MouseUpEvent
            });
            Assert.Equal(1, invokeCount);
        }
コード例 #3
0
        public void BindCommandToObjectWithEventCommandIsArgumentNull()
        {
            var vm   = new CommandBindingViewModel();
            var view = new CommandBindingView {
                ViewModel = vm
            };

            var invokeCount = 0;

            vm.Command2.Subscribe(_ => invokeCount++);
            var btn = new Button();
            var cmd = (btn as ICommand) !;
            var sub = new Subject <object>();

            Assert.Throws <TargetInvocationException>(() =>
            {
                var disp = CreatesCommandBinding.BindCommandToObject(cmd, view, sub, "PropertyChanged");

                view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
                {
                    RoutedEvent = UIElement.MouseUpEvent
                });

                disp.Dispose();

                view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
                {
                    RoutedEvent = UIElement.MouseUpEvent
                });
            });

            Assert.Equal(0, invokeCount);
        }
コード例 #4
0
            static (IDisposable, WeakReference) GetWeakReference()
            {
                var vm   = new CommandBindingViewModel();
                var view = new CommandBindingView {
                    ViewModel = vm
                };
                var weakRef = new WeakReference(vm);
                var disp    = view.BindCommand(vm, x => x.Command2, x => x.Command2, "MouseUp");

                view.ViewModel = new CommandBindingViewModel();

                return(disp, weakRef);
            }
コード例 #5
0
        public void CommandBindViewModelToViewWithFunc()
        {
            var vm   = new CommandBindingViewModel();
            var view = new CommandBindingView {
                ViewModel = vm
            };

            // Create a paramenter feed
            vm.Command2.Subscribe(_ =>
            {
                vm.Value++;
            });
            view.BindCommand(vm, x => x.Command2, x => x.Command2, "MouseUp");

            // Bind the command and the Func<T> parameter.
            var fixture = new CommandBinderImplementation().BindCommand(vm, view, vm => vm.Command1, v => v.Command3, vm => vm.Value, "MouseUp");

            Assert.Equal(0, vm.Value);

            // Confirm that the values update as expected.
            var parameter = 0;

            vm.Command1.Subscribe(i => parameter = i);
            view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
            {
                RoutedEvent = UIElement.MouseUpEvent
            });
            Assert.Equal(1, vm.Value);
            Assert.Equal(0, parameter);

            view.Command3.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
            {
                RoutedEvent = UIElement.MouseUpEvent
            });
            Assert.Equal(1, parameter);

            view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
            {
                RoutedEvent = UIElement.MouseUpEvent
            });
            Assert.Equal(2, vm.Value);
            Assert.Equal(1, parameter);

            view.Command3.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
            {
                RoutedEvent = UIElement.MouseUpEvent
            });
            Assert.Equal(2, parameter);
            Assert.Equal(2, vm.Value);
        }