一旦元素从可视化树中删除,commandBinding 的 CanExecute 如何触发?

发布于 2024-08-17 08:29:30 字数 229 浏览 5 评论 0原文

我有一个相关的问题这里,其中我有一个带有命令绑定的用户控件。用户控件已从可视化树中删除,但 canExecute 仍在触发。我对指挥模型的理解是,它像路由事件一样冒泡和隧道。那么,一旦附加了命令绑定的元素不再位于可视化树中,CanExecute 如何触发呢?

I have a related question here where i have a user control with a command binding. The user control has being removed from the visual tree, yet the canExecute is still firing. My understanding of the Commanding model was that it bubbles and tunnels like routed events. So how can the CanExecute fire once the element with the command binding attached is no longer in the visual tree?

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

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

发布评论

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

评论(2

寂寞清仓 2024-08-24 08:29:31

IMO,CommandBindings 在 WPF 中的实现确实很差。您必须解决这样一个事实:如果系统具有 CommandBinding,系统会保留对您的控件的 WeakReference,即使控件已关闭也是如此。

您将在网上看到很多有关如何在 XAML 代码中设置 CommandBinding 的示例。问题是所有这些示例都会在粘贴它们的任何应用程序中引入性能问题。 CommandBindings 永远不会自行正常消失。至少,在很长一段时间内不会。

解决方案是:

A) 不要在 XAML 中设置 CommandBindings。您必须使用后面的代码。建议在调用InitializeComponent()之后使用构造函数。使用 this.CommandBindings.Add() 通过代码添加 CommandBindings。

B) 处理窗口或控件的 Closed() 事件并调用 this.CommandBindings.Clear()。

这是我能够可靠地让 CommandBindings 停止触发的唯一方法。我认为微软以这种方式实现此功能是一种荒谬的方式。事实上,如此多的在线示例教您在 XAML 中声明 CommandBindings,这只会加剧问题。

IMO, CommandBindings are really poorly implemented in WPF. You have to work around the fact that the system keeps a WeakReference to your control if it has a CommandBinding, even when the control is closed.

You will see lots of examples online on how to set up a CommandBinding in XAML code. The problem is all these examples are going to introduce performance problems into any app where they are pasted. The CommandBindings never go away properly on their own. At least, not for a long time.

The solution is to:

A) Do not set up CommandBindings in XAML. You have to use the code behind. Suggest using the Constructor after you have called InitializeComponent(). Use this.CommandBindings.Add() to add the CommandBindings with code.

B) handle the Closed() event of your Window or Control and call this.CommandBindings.Clear().

This is the only way I have been able to reliably get the CommandBindings to quit firing. I think this is a ridiculous way for this feature to have been implemented by Microsoft. The fact that so many online examples teach you to declare CommandBindings in XAML just exacerbates the problem.

深海少女心 2024-08-24 08:29:31

我的猜测是有一个在命令管理器中注册的命令实例。命令可以从许多不同的源执行,而不仅仅是 UI(例如快捷键)。

尝试调用 CommandManager.InvalidateRequerySuggested();并在 canexecute 方法中添加一个断点以确认情况确实如此。

希望这有帮助。

My Guess is that there is an instance of the command registered with the commandmanager. Commands can be executed from many different sources not just UI for example shortcut keys.

Try calling CommandManager.InvalidateRequerySuggested(); and add a break point in your canexecute method to confirm that this is the case.

Hope this helps.

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