[英]Odd behaviour onCanExecute RelayCommand MVVMLight 5+
I'm migrating a tool from MVVM Light 4.0.3 to 5.4.1 and I have found a very odd issue with the newest RelayCommand implementation. 我正在将一个工具从MVVM Light 4.0.3迁移到5.4.1,我发现最新的RelayCommand实现有一个非常奇怪的问题。
This is the old implementation in the V4.0.3 : 这是V4.0.3中的旧实现:
This is the newest implementation in the V5.4.1 : 这是V5.4.1中的最新实现:
Before I was able to use variables to define the canExecute behaviour (enabled a button) with the following code: 在我能够使用变量来定义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;
}
The previous code was able to enable the button without any issue in the MVVM Light 4.0.3; 之前的代码能够在MVVM Light 4.0.3中启用按钮而没有任何问题; however, in the newest implementation is always disabled, I added changed a bit since there is a new definition of keepTargetAlive :
但是,在最新的实现总是被禁用,我添加了一点,因为有一个新的keepTargetAlive定义:
GetNewItemsFromDB = new RelayCommand(GetDataFromDB, () => { return IsActive == false; }, true);
Also, I tried the false option and nothing changed. 此外,我尝试了错误的选项,没有任何改变。 The only way that I found to re-enabled it, it was to set a predefined value like this one:
我发现重新启用它的唯一方法是设置一个像这样的预定义值:
GetNewItemsFromDB = new RelayCommand(GetDataFromDB, () => true, true);
This implementation is going to be useless in my case since the RelayCommand depends on the variable IsActive , which determines if it's enabled or not. 在我的情况下,这种实现将毫无用处,因为RelayCommand依赖于变量IsActive ,它决定了它是否被启用。 Does anyone what I should change in the V5 to make it work?
有什么我应该在V5中改变它以使其工作? Thanks for your suggestions.
谢谢你的建议。
If I am understanding this correctly. 如果我正确理解这一点。
If you are using this class in WPF4.5 or above, you need to use the
GalaSoft.MvvmLight.CommandWpf
namespace (instead ofGalaSoft.MvvmLight.Command
).如果您在WPF4.5或更高版本中使用此类,则需要使用
GalaSoft.MvvmLight.CommandWpf
命名空间(而不是GalaSoft.MvvmLight.Command
)。 This will enable (or restore) the CommandManager class which handles automatic enabling/disabling of controls based on the CanExecute delegate.这将启用(或恢复)CommandManager类,该类根据CanExecute委托处理控件的自动启用/禁用。
And in the release notes: 并在发行说明中:
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.
问题7659的重要说明 :为了解决不再禁用控件的问题,具体取决于RelayCommand.CanExecute委托的状态,您需要对代码进行一些小的更改。 To opt-in into the fixed behavior, please change the namespace you are using from
GalaSoft.MvvmLight.Command
toGalaSoft.MvvmLight.CommandWpf
.要选择加入固定行为,请将您使用的命名空间从
GalaSoft.MvvmLight.Command
为GalaSoft.MvvmLight.CommandWpf
。
I remember correctly, somewhere in ancient history I had to do this myself for a project. 我没记错,在古代历史的某个地方,我必须自己做一个项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.