CommandManager.InvalidateRequerySuggested 不会导致对 MVVM-Light 中的 CanExecute 进行重新查询
我正在使用 MVVM-Light RelayCommand
private ICommand myRevertCmd;
public ICommand Revert
{
get
{
if (myRevertCmd == null)
{
myRevertCmd = new RelayCommand(RevertExecute, CanRevertExecute);
}
return myRevertCmd;
}
}
private void RevertExecute()
{
searchType = SearchType.Revert;
SearchStart();
}
private bool CanRevertExecute()
{
return isRevertEnabled;
}
我有一些代码可以更改 isRevertEnabled 的值,但链接的按钮不会更改。经过一番搜索后,我发现您可以使用强制重新评估按钮状态
// force the GUI to re-evaluate the state of the buttons
CommandManager.InvalidateRequerySuggested();
但这不起作用。有人有什么建议吗?
I am using MVVM-Light RelayCommand
private ICommand myRevertCmd;
public ICommand Revert
{
get
{
if (myRevertCmd == null)
{
myRevertCmd = new RelayCommand(RevertExecute, CanRevertExecute);
}
return myRevertCmd;
}
}
private void RevertExecute()
{
searchType = SearchType.Revert;
SearchStart();
}
private bool CanRevertExecute()
{
return isRevertEnabled;
}
I have some code that changes the value of isRevertEnabled but the linked button does not change. After some searching I found that you can use to force the re-evaluation of the button states
// force the GUI to re-evaluate the state of the buttons
CommandManager.InvalidateRequerySuggested();
But this doesn't work. Does any one have any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
只是为了添加另一个可能的解决方案,在我的例子中,我需要使用
Application.Current.Dispatcher.Invoke
在 UI 线程上调用CommandManager.InvalidateRequerySuggested
。Just to add another possible solution, in my case I needed to call
CommandManager.InvalidateRequerySuggested
on the UI thread usingApplication.Current.Dispatcher.Invoke
.有很多建议(此处, 此处,此处)。
我使用一个简单但不太漂亮的解决方法。我只需在
BackgroundWorker
Completed 事件中调用OnPropertyChanged("MyICommand")
来获取命令。There are a lot suggestions out there (here, here, here).
I use a simple but not so beautiful workaround. I simply call
OnPropertyChanged("MyICommand")
for my commands in myBackgroundWorker
Completed Event.根据 Josh Smith 的文章 '允许 CommandManager查询您的 ICommand 对象'。问题在于该命令是非路由命令。
我对 MVVM-Light RelayCommand 进行了新的实现,如下所示:
According to Josh Smith's article 'Allowing CommandManager to query your ICommand objects'. The problem is that the command is a non-routed command.
I have made a new implementation of the MVVM-Light RelayCommand as follows:
我会将 isRevertEnabled 标志转换为属性,并从那里调用 OnPropertyChanged 方法(您需要实现 INotifyPropertyChanged 接口)。在更改标志时,您需要使用该属性,例如 IsRevertEnabled = true。
I would turn the isRevertEnabled flag into a property and call the OnPropertyChanged method from there (you need to implement the INotifyPropertyChanged interface). At the point where you change the flag, you need to use the property, e.g. IsRevertEnabled = true.
我想你可以在“
isRevertEnabled
”INPC(或依赖属性)设置方法中调用“Revert.RaiseCanExecuteChanged()
”方法。这将强制执行“CanRevertExecute
”谓词。I guess you can call "
Revert.RaiseCanExecuteChanged()
" method at "isRevertEnabled
" INPC (or dependency property) set method. This will force the "CanRevertExecute
" predicate.只需调用
Revert.RaiseCanExecuteChanged();
Just call
Revert.RaiseCanExecuteChanged();
@heltonbiker 已经在评论中指出了这一点,但解决方案是更改名称空间。
如果您查看
RelayCommand
的源代码,您会看到以下注释:@heltonbiker already pointed this out in comments, but the solution is to change namespaces.
If you look at the source for
RelayCommand
, you see the following remark: