奇怪的行为 onCe Execute RelayCommand MVVM Light 5+

Odd behaviour onCanExecute RelayCommand MVVMLight 5+

我正在将一个工具从 MVVM Light 4.0.3 迁移到 5.4.1,我发现最新的 RelayCommand 实现有一个非常奇怪的问题。

这是 V4.0.3 中的旧实现:

这是 V5.4.1 中的最新实现:

在我能够使用变量通过以下代码定义 canExecute 行为(启用按钮)之前:

public ICommand GetNewItemsFromDB { get; private set; }

private bool _IsActive;
public bool IsActive
{
    get
    {
        return _IsActive;
    }
    set
    {
        if (_IsActive != value)
        {
            _IsActive = value;
            this.RaisePropertyChanged(() => IsActive);
        }
    }
}

GetNewItemsFromDB = new RelayCommand(GetDataFromDB, () => { return IsActive == false; });

private void GetDataFromDB()
{
    IsActive = true;
}

之前的代码能够在 MVVM Light 4.0.3 中毫无问题地启用按钮;然而,在最新的实现中总是被禁用,我添加了一些更改,因为有一个新的定义 keepTargetAlive:

GetNewItemsFromDB = new RelayCommand(GetDataFromDB, () => { return IsActive == false; }, true);

此外,我尝试了 false 选项,但没有任何改变。我发现重新启用它的唯一方法是设置一个像这样的预定义值:

GetNewItemsFromDB = new RelayCommand(GetDataFromDB, () => true, true);

这个实现在我的例子中将毫无用处,因为 RelayCommand 取决于变量 IsActive,它决定它是否启用。有没有人我应该在 V5 中改变什么才能让它工作?感谢您的建议。

如果我理解正确的话。

If you are using this class in WPF4.5 or above, you need to use the GalaSoft.MvvmLight.CommandWpf namespace (instead of GalaSoft.MvvmLight.Command). This will enable (or restore) the CommandManager class which handles automatic enabling/disabling of controls based on the CanExecute delegate.

在发行说明中:

Important note about issue 7659: In order to fix the issue where the controls are not disabled anymore depending on the state of the RelayCommand.CanExecute delegate, you need to make a small change into your code. To opt-in into the fixed behavior, please change the namespace you are using from GalaSoft.MvvmLight.Command to GalaSoft.MvvmLight.CommandWpf.

我没记错,在古代历史的某个地方,我不得不自己为一个项目做这件事。