[英]Disable mouse clicks during long-running process?
I have an issue where when a button is clicked and the process takes a while, the user clicks on other locations on the GUI screen. 我有一个问题,当点击一个按钮并且该过程需要一段时间时,用户点击GUI屏幕上的其他位置。 Those button clicks get queued up in the dispatcher so after the initial button click those other locations get clicked. 这些按钮点击在调度程序中排队,因此在初始按钮单击后,单击其他位置。 I want to prevent this from happening so other UI controls don't get clicked after it's done processing. 我希望防止这种情况发生,因此其他UI控件在完成处理后不会被点击。
The only solution I can think of is to p/invoke into the mouse events and prevent them from being passed to the application while the button click is being processed. 我能想到的唯一解决方案是在处理按钮点击时调用鼠标事件并阻止它们传递给应用程序。 Is this the best approach? 这是最好的方法吗?
One of the easier solutions to this is to have some sort of semi-transparent overlay (potentially with progress information) that you pop up when there are long-running tasks going on. 对此更简单的解决方案之一是在有长时间运行的任务时弹出一些半透明覆盖(可能包含进度信息)。 That will reliably disable the entire UI until you remove the overlay (which keeps you from having to do a lot of changing/binding IsEnabled properties). 这将可靠地禁用整个UI,直到您删除覆盖(这使您不必进行大量更改/绑定IsEnabled属性)。
If the process takes a while, it is generally recommended to perform such process in a worker thread, rather than in a GUI thread. 如果该过程需要一段时间,通常建议在工作线程中而不是在GUI线程中执行此类过程。 So use BackgroundWorker
approach like in this CodeProject article , and then use CanExecute
method of commands associated with specified buttons, like here: 因此,在此CodeProject文章中使用BackgroundWorker
方法,然后使用与指定按钮关联的命令的CanExecute
方法,如下所示:
WPF Commanding – When do Commands re-evaluate their CanExecute method? WPF命令 - 何时命令重新评估其CanExecute方法?
WPF – CanExecute refreshed WPF - CanExecute刷新
EDIT: 编辑:
You can bind IsEnabled
property of your other controls to the result of Command's CanExecute method. 您可以将其他控件的IsEnabled
属性绑定到Command的CanExecute方法的结果。
How to disable combobox when command canExecute returns false 如何在命令canExecute返回false时禁用组合框
How to bind a ComboBoxItem's IsEnabled property to the result of a Command's CanExecute method 如何将ComboBoxItem的IsEnabled属性绑定到Command的CanExecute方法的结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.