예제 #1
0
        public void CommandBindByNameWireup()
        {
            var vm   = new WinformCommandBindViewModel();
            var view = new WinformCommandBindView {
                ViewModel = vm
            };
            var fixture = new CommandBinderImplementation();

            var invokeCount = 0;

            vm.Command1.Subscribe(_ => invokeCount += 1);

            var disp = fixture.BindCommand(vm, view, x => x.Command1, x => x.Command1);

            view.Command1.PerformClick();

            Assert.Equal(1, invokeCount);

            var newCmd = ReactiveCommand.Create(() => { });

            vm.Command1 = newCmd;

            view.Command1.PerformClick();
            Assert.Equal(1, invokeCount);

            disp.Dispose();
        }
예제 #2
0
        public void CommandBindToExplicitEventWireup()
        {
            var vm   = new CommandBindViewModel();
            var view = new CommandBindView()
            {
                ViewModel = vm
            };
            var fixture = new CommandBinderImplementation();

            int invokeCount = 0;

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

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

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

            disp.Dispose();

            view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
            {
                RoutedEvent = Image.MouseUpEvent
            });
            Assert.Equal(1, invokeCount);
        }
예제 #3
0
        public void CommandBindRaisesCanExecuteChangedOnBind()
        {
            var vm   = new CommandBindViewModel();
            var view = new CommandBindView()
            {
                ViewModel = vm
            };
            var fixture = new CommandBinderImplementation();

            var canExecute1 = new BehaviorSubject <bool>(true);
            var cmd1        = new ReactiveCommand(canExecute1);

            vm.Command1 = cmd1;

            var disp = fixture.BindCommand(vm, view, x => x.Command1, x => x.Command1);

            Assert.True(view.Command1.IsEnabled);

            // Now  change to a disabled cmd

            var canExecute2 = new BehaviorSubject <bool>(false);
            var cmd2        = new ReactiveCommand(canExecute2);

            vm.Command1 = cmd2;

            Assert.False(view.Command1.IsEnabled);
        }
        public void CommandBindToExplicitEventWireupWithParameter()
        {
            var vm   = new WinformCommandBindViewModel();
            var view = new WinformCommandBindView {
                ViewModel = vm
            };
            var fixture = new CommandBinderImplementation();

            var invokeCount = 0;

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

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

            view.Command2.RaiseMouseUpEvent(new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0));
            Assert.Equal(10, vm.ParameterResult);
            Assert.Equal(1, invokeCount);

            vm.Parameter = 2;
            view.Command2.RaiseMouseUpEvent(new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0));
            Assert.Equal(20, vm.ParameterResult);
            Assert.Equal(2, invokeCount);

            disp.Dispose();

            view.Command2.RaiseMouseUpEvent(new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0));
            Assert.Equal(2, invokeCount);
        }
        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);
        }
예제 #6
0
        public void CommandBindNestedCommandWireup()
        {
            var vm = new CommandBindViewModel()
            {
                NestedViewModel = new FakeNestedViewModel()
            };

            var view = new CommandBindView {
                ViewModel = vm
            };
            var fixture = new CommandBinderImplementation();

            var disp = fixture.BindCommand(vm, view, m => m.NestedViewModel.NestedCommand, x => x.Command1);

            Assert.Equal(vm.NestedViewModel.NestedCommand, view.Command1.Command);
        }
예제 #7
0
        public void CommandBindSetsInitialEnabledState_False()
        {
            var vm   = new CommandBindViewModel();
            var view = new CommandBindView()
            {
                ViewModel = vm
            };
            var fixture = new CommandBinderImplementation();

            var canExecute1 = new BehaviorSubject <bool>(false);
            var cmd1        = new ReactiveCommand(canExecute1);

            vm.Command1 = cmd1;

            var disp = fixture.BindCommand(vm, view, x => x.Command1, x => x.Command1);

            Assert.False(view.Command1.IsEnabled);
        }
예제 #8
0
        public void CommandBindToExplicitEventWireup()
        {
            var vm   = new WinformCommandBindViewModel();
            var view = new WinformCommandBindView {
                ViewModel = vm
            };
            var fixture = new CommandBinderImplementation();

            var invokeCount = 0;

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

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

            view.Command2.RaiseMouseUpEvent(new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0));

            disp.Dispose();

            view.Command2.RaiseMouseUpEvent(new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0));
            Assert.Equal(1, invokeCount);
        }
예제 #9
0
        public void CommandBindByNameWireup()
        {
            var vm   = new CommandBindViewModel();
            var view = new CommandBindView()
            {
                ViewModel = vm
            };
            var fixture = new CommandBinderImplementation();

            Assert.Null(view.Command1.Command);

            var disp = fixture.BindCommand(vm, view, x => x.Command1, x => x.Command1);

            Assert.Equal(vm.Command1, view.Command1.Command);

            var newCmd = new ReactiveCommand();

            vm.Command1 = newCmd;
            Assert.Equal(newCmd, view.Command1.Command);

            disp.Dispose();
            Assert.Null(view.Command1.Command);
        }
예제 #10
0
        public void CommandBindSetsDisablesCommandWhenCanExecuteChanged()
        {
            var vm   = new CommandBindViewModel();
            var view = new CommandBindView()
            {
                ViewModel = vm
            };
            var fixture = new CommandBinderImplementation();

            var canExecute1 = new BehaviorSubject <bool>(true);
            var cmd1        = new ReactiveCommand(canExecute1);

            vm.Command1 = cmd1;


            var disp = fixture.BindCommand(vm, view, x => x.Command1, x => x.Command1);

            Assert.True(view.Command1.IsEnabled);

            canExecute1.OnNext(false);

            Assert.False(view.Command1.IsEnabled);
        }
        public void CommandBindByNameWireupWithParameter()
        {
            var vm   = new WinformCommandBindViewModel();
            var view = new WinformCommandBindView {
                ViewModel = vm
            };
            ICommandBinderImplementation fixture = new CommandBinderImplementation();

            var invokeCount = 0;

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

            var disp = CommandBinderImplementationMixins.BindCommand(fixture, vm, view, vm => vm.Command3, v => v.Command1, vm => vm.Parameter);

            view.Command1.PerformClick();
            Assert.Equal(1, invokeCount);
            Assert.Equal(10, vm.ParameterResult);

            // update the parameter to ensure its updated when the command is executed
            vm.Parameter = 2;
            view.Command1.PerformClick();
            Assert.Equal(2, invokeCount);
            Assert.Equal(20, vm.ParameterResult);

            // break the Command3 subscription
            var newCmd = ReactiveCommand.Create <int>(i => vm.ParameterResult = i * 2);

            vm.Command3 = newCmd;

            // ensure that the invoke count does not update and that the Command3 is now using the new math
            view.Command1.PerformClick();
            Assert.Equal(2, invokeCount);
            Assert.Equal(4, vm.ParameterResult);

            disp.Dispose();
        }