即使CanExecute为false,也始终会调用Execute,这是正确的吗?

时间:2010-06-10 20:59:22

标签: c#

我正在使用委托命令。 我注意到无论CanExecute是true还是false,总是会调用execute。 它是否正确? 我原以为只有在CanExecute为真时才会调用Execute。

你能说清楚吗?

非常感谢

EDITED测试显示Save总是被称为

             [TestFixture]
                public class Can_test_a_method_has_been_called_via_relay_command
                {
                    [Test]
                    public void Should_be_able_to_test_that_insert_method_has_been_called_on_repository()
                    {
                        var mock = new Mock<IEmployeeRepository>();
                        var employeeVm = new EmployeeVM(mock.Object) {Age = 19};
                        employeeVm.SaveCommand.Execute(null);
                        mock.Verify(e=>e.Insert(It.IsAny<Employee>()));
                    }
                    [Test]
                    public void Should_be_able_to_test_that_insert_method_has_not_been_called_on_repository()
                    {
                        var mock = new Mock<IEmployeeRepository>();
                        var employeeVm = new EmployeeVM(mock.Object) { Age = 15 };
                        employeeVm.SaveCommand.Execute(null);
                        mock.Verify(e => e.Insert(It.IsAny<Employee>()),Times.Never());
                    }
                }

                public class EmployeeVM:ViewModelBase
                {
                    private readonly IEmployeeRepository _employeeRepository;

                    public EmployeeVM(IEmployeeRepository employeeRepository)
                    {
                        _employeeRepository = employeeRepository;
                    }


                    private bool _hasInserted;
                    public bool HasInserted
                    {
                        get { return _hasInserted; }
                        set
                        {
                            _hasInserted = value;
                            OnPropertyChanged("HasInserted");
                        }
                    }

                    private int _age;
                    public int Age
                    {
                        get { return _age; }
                        set
                        {
                            _age = value;
                            OnPropertyChanged("Age");
                        }
                    }
                    private string _name;
                    public string Name
                    {
                        get { return _name; }
                        set
                        {
                            _name = value;
                            OnPropertyChanged("Name");
                        }
                    }
                    private RelayCommand _saveCommand;
                    public ICommand SaveCommand
                    {
                        get
                        {
                            return _saveCommand ?? (_saveCommand = new RelayCommand(x => Save(), x => CanSave));
                        }
                    }
                    private  bool CanSave
                    {
                        get 
                        {
                            return Age > 18;
                        }
                    }

                    private void Save()
                    {
                        Insert();
                        HasInserted = true;
                    }

                    private void Insert()
                    {
                        _employeeRepository.Insert(new Employee{Age = Age,Name = Name});
                    }
                }

                public interface IEmployeeRepository
                {
                    void Insert(Employee employee);
                }

                public class Employee
                {
                    public string Name { get; set; }
                    public int Age { get; set; }
                }
            }

1 个答案:

答案 0 :(得分:1)

您的测试方法没有测试WPF将在运行时做什么。

WPF将首先确定CanExecute是否计算为true - 如果不是,则禁用Button / MenuItem / InputBinding等,因此无法触发。

正如我在评论中提到的那样 - 这只是按惯例强制执行。