如何拦截视图中 RoutedCommand 的执行?

发布于 2024-09-09 03:13:29 字数 279 浏览 1 评论 0原文

在 WPF / MVVM 应用程序中,我试图找到一种代码高效的方法,在任何冗长的操作之前将主视图光标设置为 Cursors.Wait ,并在完成后将其恢复为默认光标。

由于所有操作都是使用路由命令调用的,因此我想找到一种拦截命令执行的方法,最好是通过创建一个通用命令类来包装内置路由命令的实例,但我无法想象如何做这。

具体来说,RoatedCommand.Execute 方法不是虚拟的,因此我需要另一种机制来拦截其调用。另外,我不确定通用命令类的实例如何引用它必须设置光标的视图。

请问有什么建议吗?

In a WPF / MVVM application, I am trying to find a code-efficient way to set the main view cursor to Cursors.Wait prior to any lengthy operation and to restore it to the default cursor after completion.

Since all operations are invoked using routed commands, I'd like to find a way of intercepting command execution, ideally by creating a generic command class that wraps an instance of a built-in routed command, but I can't visualize how to do this.

Specifically, the RoutedCommand.Execute method is not virtual so I need another mechanism to intercept its calls. Also, I am not sure how an instance of the generic command class would reference the view for which it has to set the cursor.

Any advice please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌化 2024-09-16 03:13:29

您可以以静态方式设置光标。效果是,当应用程序获得焦点时,而不是在某个控件上时,光标将处于“等待”状态。

代码,在冗长的操作开始时将成为 ViewModel 的一部分:

Mouse.OverrideCursor = Cursors.Wait;

操作完成后,您必须像这样清除覆盖:

Move.OverrideCursor = null;

You can set the Cursor in a static manner. The effect is that the cursor will be 'Wait' while the app has focus rather than when it is over a certain control.

The code, which would be part of the ViewModel at the start of the lengthy operation:

Mouse.OverrideCursor = Cursors.Wait;

After the operation is complete you have to clear the override like this:

Move.OverrideCursor = null;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文