[英]DelegateCommand.CanExecute method not behaving correctly
I'm binding a control to a DelegateCommand and the CanExecute portion of it is not working properly. 我将控件绑定到DelegateCommand,并且其CanExecute部分无法正常工作。 I am using the Prism libraries.
我正在使用Prism库。 Can anyone tell me why?
谁能告诉我为什么?
Command declaration and instantiation: 命令声明和实例化:
public PlayerManagementViewModel(DatabaseManager dbManager)
{
_dbManager = dbManager;
this.ResetUpToDateStatusCommand = new DelegateCommand(() => this.ResetXpUpToDateStatus());
this.DeletePlayerCommand = new DelegateCommand(() => this.DeleteSelectedPlayer(), () => SelectedPlayer != null);
this.RefreshPlayers();
}
public ICommand DeletePlayerCommand { get; private set; }
SelectedPlayer definition: SelectedPlayer定义:
public Player SelectedPlayer
{
get { return _selectedPlayer; }
set
{
SetProperty(ref this._selectedPlayer, value);
this.OnPropertyChanged(() => this.FormattedPlayerStatus);
}
}
The weird thing is that if you look at the line above the DeletePlayerCommand
instantiation, that line works just fine. 奇怪的是,如果您查看
DeletePlayerCommand
实例化上方的DeletePlayerCommand
行,那行就可以了。 I don't get any CanExecute
behavior out of it, but at least it works. 我没有得到任何
CanExecute
行为,但至少它能起作用。 As is, the DeletePlayerCommand
command never fires off, even with a breakpoint, unless I remove the CanExecute portion of the constructor entirely. DeletePlayerCommand
,即使没有断点, DeletePlayerCommand
命令也永远不会触发,除非我完全删除了构造函数的CanExecute部分。
Can anyone please explain to me why this is or what I'm doing wrong? 谁能向我解释这是为什么还是我做错了什么?
If the CanExecute
function of the DeletePlayerCommand
is () => SelectedPlayer != null
, then there must be a DelegateCommandBase.RaiseCanExecuteChanged Method call when SelectedProperty
value is changed: 如果
CanExecute
的功能DeletePlayerCommand
是() => SelectedPlayer != null
,则必须有一个DelegateCommandBase.RaiseCanExecuteChanged方法打电话时SelectedProperty
值改变:
Raises CanExecuteChanged on the UI thread so every command invoker can requery to check if the command can execute.
在UI线程上引发CanExecuteChanged,以便每个命令调用者都可以重新查询以检查命令是否可以执行。
The appropriate UI-element (with data-bound command) is a command invoker. 适当的UI元素(带有数据绑定命令)是命令调用程序。
To summarize, the implementation of the SelectedPlayer
property should be updated as follows: 总而言之,
SelectedPlayer
属性的实现应更新如下:
class PlayerManagementViewModel : BindableBase
{
private Player _selectedPlayer;
private readonly DelegateCommand _deletePlayerCommand;
public PlayerManagementViewModel(...)
{
_deletePlayerCommand = new DelegateCommand(() => DeleteSelectedPlayer(), () => SelectedPlayer != null);
}
public ICommand DeletePlayerCommand
{
get { return _deletePlayerCommand; }
}
public Player SelectedPlayer
{
get { return _selectedPlayer; }
set
{
SetProperty(ref _selectedPlayer, value);
OnPropertyChanged(() => FormattedPlayerStatus);
_deletePlayerCommand.RaiseCanExecuteChanged();
}
}
}
It is the way PRISM DelegateCommnd is designed. 这是PRISM DelegateCommnd的设计方式。 refer CanExecuteChanged event of ICommand .
请参阅ICommand的CanExecuteChanged事件 。
Alternatively you can derive the DelegateCommand to overcome the limitation. 或者,您可以派生DelegateCommand来克服该限制。 refer the below code.
请参考以下代码。
class DelegateCmdEx : DelegateCommand
{
public DelegateCmdEx(Action executeMethod):base(executeMethod)
{
}
public DelegateCmdEx(Action executeMethod, Func<bool> canExecuteMethod)
: base(executeMethod, canExecuteMethod)
{
}
public override event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
}
remove
{
CommandManager.RequerySuggested -= value;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.