WPF 如何使用 CommandManager (CommandManager.InvalidateRequerySuggested) 确定何时无效?
我一直在使用 RelayCommand ,它让我想知道并想知道...... CommandManager.InvalidateRequerySuggested()
< /a> 被 WPF 调用?
我的猜测是每次 UI 上的任何其他路由事件被触发时,这在 MVVM 世界中主要意味着当引发 OnPropertyChanged 事件时,导致值发生变化,从而导致执行一些相关的路由事件。但是,这只是一个猜测,我想知道是否有人知道这里确切的内部工作原理。
I've been using the RelayCommand a bit here and there, and it got me to wonder and want to know... when exactly does the CommandManager.InvalidateRequerySuggested()
get called by WPF?
My guess is every time any other routed event on UI get's triggered, which in the MVVM world primarily means when the the OnPropertyChanged event is raised, causing the value to change, causing some related routedevent to execute. But, that's just a guess, I'm wondering if anyone knows the exact inner workings here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道有几个原因:
当键盘焦点改变时。
命令执行后。
控件可以在其他一些情况下认为有必要时调用此方法(例如,当您在 TextBox 中按下某个键时)。
属性更改本身通常并不是失效的原因(我猜是出于性能原因)。
遗憾的是,我在 MSDN 中没有找到任何关于此的文档。我在这里发布的内容基于我在 Pro WPF in C# 2010 中找到的内容(顺便说一句,很棒的书)。
此外,失效的并不是 CommandManager。 CommandManager 仅发送一个事件(使用命令的控件订阅该事件),以便它们可以更新其启用/禁用状态。
There are a few causes I know of:
When keyboard focus changes.
After a command is executed.
Controls can invoke this when as they deem necessary in some other cases (for example when you press a key while in TextBox).
Property change alone is not generally a reason for invalidation (I guess for performance reasons).
Sadly, I failed to find any documentation about this in MSDN. What I posted here is based on what I found in Pro WPF in C# 2010 (great book BTW).
Also, its not the CommandManager that gets invalidated. The CommandManager merely sends an event (to which controls using commands subscribe), so they can update their enabled/disabled status.