ICommand 和“CanExecuteMethod”为什么它对我们不起作用?
我们正在尝试使用 ICommand 在 Silverlight 和 Prism 中设置按钮。我们希望有时禁用该按钮。 DelegateCommand 有 2 个参数,一个“ExecuteMethod”和一个“CanExecuteMethod”。
当我们设置 ICommand 时,我们期望如果使用“CanExecuteMethod”,那么将调用它来查看是否可以调用“ExecuteMethod”。按钮的启用状态应该反映“CanExecuteMethod”的结果,
我们实际看到的是: 创建表单后,将调用该方法并启用或禁用该按钮。 (在本例中,已启用) 尽管我们尝试设置行为来防止这种情况发生,但 CanExecuteMethod 永远不会再次被调用,并且 Execute 方法将被触发。抛出异常(我们希望避免的情况)。
显而易见的答案是我们应该调用某种 : ,
OnPropertyChanged("SaveCommand");
但不知何故我们做错了。要么我们假设它有效,但事实并非如此,要么我们遗漏了一个步骤。有什么想法吗?
代码:
SaveCommand = new DelegateCommand<string>(OnSaveCommand, CanSave);
public void OnSaveCommand( string helpNumber )
{
OnPropertyChanged("SaveCommand");
//DoSaveStuff
}
public bool CanSave(Object sender)
{
return Model.CanSave();// true or false depending
}
We're trying to use an ICommand to set up a button in Silverlight with Prism. We'd like the button to be disabled on occasion. DelegateCommand takes 2 parameters, an "ExecuteMethod" and a "CanExecuteMethod"
When we set up the ICommand we expect that if the "CanExecuteMethod" is used, then it will be called to see if the "ExecuteMethod" can be called. The button's Enabled state should reflect the result of the "CanExecuteMethod"
What we actually see:
When the form is created, the method is called and the button is enabled or disabled. (in this case, Enabled)
The CanExecuteMethod is never called again and the Execute method will fire even though we've tried to set behavior to keep that from happening. Execption is thrown (what we'd like to avoid).
The obvious answer is that we should be calling some sort of :
OnPropertyChanged("SaveCommand");
but we're doing it wrong somehow. Either we're assuming that it works a way that it doesn't, or we're missing a step. Any Ideas?
Code:
SaveCommand = new DelegateCommand<string>(OnSaveCommand, CanSave);
public void OnSaveCommand( string helpNumber )
{
OnPropertyChanged("SaveCommand");
//DoSaveStuff
}
public bool CanSave(Object sender)
{
return Model.CanSave();// true or false depending
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 SaveCommand 因为它是 DelegateCommand,所以有一个名为 RaiseCanExecuteChanged() 的函数。
当您调用此函数时,它将通过 CanSave 函数刷新控件。
DelegateCommands 的 OnPropertyChanged 等于 MyCommand.RaiseCanExecuteChanged。
玩得开心!
Your SaveCommand, because it is a DelegateCommand, has a function called RaiseCanExecuteChanged().
When you call this function it will have the control refresh from the CanSave function.
The OnPropertyChanged equal for DelegateCommands is MyCommand.RaiseCanExecuteChanged.
Have fun!