简体   繁体   English

为什么 CanExecute 只有在视图模型的构造函数中初始化时才会被调用?

[英]Why CanExecute only get called when it's initialized in the constructor of the view model?

I have a Command property, with this definition in the view model:我有一个Command属性,在视图模型中有这个定义:

public RelayCommand SaveCommand => new RelayCommand(OnSave, CanSave);

whenever the ErrorsChanged of the INotifyDataErrorInfo is fired, RaiseCanExecuteChanged is called in the RelayCommand class, to enable/disable the button:每当ErrorsChanged中的INotifyDataErrorInfo被激发, RaiseCanExecuteChanged是所谓的RelayCommand类,启用/禁用按钮:

public void RaiseCanExecuteChanged()
        {
            CanExecuteChanged(this, EventArgs.Empty);
        }

the two delegates of the command are set in the constructor:命令的两个委托在构造函数中设置:

public RelayCommand(Action executeMethod, Func<bool> canExecuteMethod)
        {
            _TargetExecuteMethod = executeMethod;
            _TargetCanExecuteMethod = canExecuteMethod;
        }

but when the error state changed (when RaiseCanExecuteChanged is called) the CanSave method doesn't get called, after a while I changed the Command initialization way to be set in the constructor instead:但是当错误状态改变时(当RaiseCanExecuteChanged被调用时) CanSave方法不会被调用,过了一会儿我改变了在构造函数中设置的Command初始化方式:

    public AddEditCustomerViewModel()
    {
        SaveCommand = new RelayCommand(OnSave, CanSave);
    }
    public RelayCommand SaveCommand {get;}

and it works!它有效! but why?但为什么?

When you initialize the SaveCommad in the view model's constructor, it only run once when the view model is initialized, but When you define SaveCommand using:当您在视图模型的构造函数中初始化SaveCommad时,它只会在视图模型初始化时运行一次,但是当您使用以下命令定义SaveCommand

public RelayCommand SaveCommand => new RelayCommand(OnSave, CanSave);

You are actually doing:你实际上是在做:

public RelayCommand SaveCommand {
    get {
        return new RelayCommand(OnSave, CanSave);
    }
}

In such case Whenever the getter of SaveCommand is called, it returns a new instance of RelayCommand .在这种情况下,每当SaveCommand的 getter 被调用时,它都会返回一个RelayCommand的新实例。 So when the RaiseCanExecuteChanged is called, it may not be the same object that's currently binding to the UI, hence failed to update the state因此,当调用RaiseCanExecuteChanged时,它可能不是当前绑定到 UI 的同一对象,因此无法更新状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在WCF反序列化器初始化对象时不调用我的抽象基类的构造函数? - Why is my abstract base class's constructor not called when an object is initialized by the WCF deserializer? 什么时候调用 CanExecute? - When is CanExecute called? 为什么在构造函数内部声明和初始化的变量已经在外部以相同的名称声明时被视为不同的变量? - Why is a variable declared and initialized inside a constructor treated as a different variable when it's already declared outside with the same name? 属性更改时,不会调用按钮命令CanExecute - Button Command CanExecute not called when property changed MVVM视图仅在viewmodel的构造函数中设置数据时更新 - MVVM view only updating when data set in viewmodel's constructor 为什么父母的构造函数被调用? - Why is parent's constructor getting called? 为什么以相反的顺序调用构造函数? - Why the constructor get called in the reverse order? 在构造函数内部时,View Model不更新UI - View Model not updating UI when inside constructor 即使CanExecute为false,也总是调用Execute,这是正确的吗? - Execute is always called even when CanExecute is false,Is this correct? 为什么在需要单参数构造函数时调用两个参数构造函数? - Why is the two parameter constructor called when single parameter constructor is expected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM