WPF。 RelayCommand - CanExecute false,当 Execute 正在处理时
我想在处理按钮的命令时禁用该按钮。
public ICommand Search { get; set; }
private void InitilizeSearchCommand()
{
Search = new RelayCommand<string>(
param => DoSearch(param),
param => !_isSearchInProgress);
}
如何修改 _isSearchInProgress?我无法在“执行”委托中执行此操作,因为它从无法访问该字段的位置(RelayCommand 对象)执行(如果我的理解是正确的):
Search = new RelayCommand<string>(
param =>
{
_isSearchInProgress = true;
DoSearch(param);
_isSearchInProgress = false;
},
param => !_isSearchInProgress);
提前感谢您的帮助。
I want to disable a button, while its command is processing.
public ICommand Search { get; set; }
private void InitilizeSearchCommand()
{
Search = new RelayCommand<string>(
param => DoSearch(param),
param => !_isSearchInProgress);
}
How can I modify _isSearchInProgress? I could not do it inside "Execute" delegate because it executes from place (RelayCommand object) where the field is not accessible (if my understanding is true):
Search = new RelayCommand<string>(
param =>
{
_isSearchInProgress = true;
DoSearch(param);
_isSearchInProgress = false;
},
param => !_isSearchInProgress);
Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决问题的方案:
应该
添加到CanExecute方法中,而不是添加到Execute中。 _isSearchInProgress 在 DoSearch() 内部更新,该 DoSearch() 在单独的线程中运行(在我的例子中为 backgroundWorker.RunWorkerAsync)。
The solution to solve the problem:
The
should be added to CanExecute method, not in Execute. _isSearchInProgress is updated inside DoSearch() that is run in separate thread (backgroundWorker.RunWorkerAsync in my case).
除非您在后台线程或任务中运行,否则 GUI 不会要求 CanExecute 部分进行更新。如果您在后台执行操作,只需确保 _isSearchInProgress 不是在 any 函数内部创建的,而是类的一部分。
Unless you are running in a background thread or Task, the GUI won't be asking for updates from the CanExecute part. If you are doing stuff in the background, just make sure _isSearchInProgress is not created inside the any function, but part of the class.