WPF 命令与事件的优缺点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2952956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:49:24  来源:igfitidea点击:

WPF Commands vs Events Advantages/Disadvantages

wpf

提问by WPF User

Can anyone tell me what are the advantages of using Commands vs Events in WPF. Do Commands or Events run into memory leaks? What is the fastest approach. What are their disadvantages.

谁能告诉我在 WPF 中使用命令与事件的优点是什么。命令或事件是否遇到内存泄漏?最快的方法是什么。它们的缺点是什么。

回答by Andrii

Commands provide two main benefits over event handlers:

与事件处理程序相比,命令有两个主要优点:

  1. commands are not linked to the caller, so same command is not dependent and can be called from menu item, toolbar button, keyboard, etc.
  2. commands provide support for enabling/disabling all related UI controls based on the status of command (can be executed or not)
  1. 命令不链接到调用者,因此相同的命令不相关,可以从菜单项、工具栏按钮、键盘等调用。
  2. 命令提供支持基于命令的状态启用/禁用所有相关的 UI 控件(可以执行或不执行)

I'd prefer using commands at real project, especially if you want to use M-V-VM.

我更喜欢在实际项目中使用命令,特别是如果你想使用 MV-VM。

I haven't heard about any memory leaks related with commands.

我还没有听说过任何与命令相关的内存泄漏。

Events are probably faster, but the difference should not be significant - I've been using commands on my projects for 2 years and hadn't any performance issues with them.

事件可能更快,但差异应该不大 - 我已经在我的项目中使用命令 2 年了,并且没有任何性能问题。

For more details on commands see Commanding Overview(archive)(v4)

有关命令的更多详细信息,请参阅命令概述存档)(v4

回答by T.J.Kjaer

But although Commands and Events can be overlapping, they are two different things. Commands say "do this!", while events say "this just happened!". So you might have a CloseWindowCommand for closing a window, but the window might have a ClosingEvent that tells subscribing objects that is is closing.

但是尽管命令和事件可以重叠,但它们是两种不同的东西。命令说“做这个!”,而事件说“这刚刚发生!”。因此,您可能有一个 CloseWindowCommand 用于关闭窗口,但该窗口可能有一个 ClosingEvent 来通知正在关闭的订阅对象。

回答by Nima Rikhtegar

Commands are a more standard way to integrate events. the can be more usefull than events because with the help of them you can define a single task (command) and use it from different places. for example you can define a save command and use a menu item, a context menu item and a button to use it at the same time. this way you can centerlize the tasks. also commands support data-binding which is a very powerfull feature of WPF application. as far as I know, commands lead to certain kinds of memory leaks but you can avoid that by using many work arounds. I must add that MVVM design pattern also uses commands as a standard way to design WPF application. working with events is much simpler but commands provide much powerfull design. but you must now that you can't always use commands instead of events. there any many places that you can only use events.

命令是一种更标准的集成事件的方式。可能比事件更有用,因为在它们的帮助下,您可以定义单个任务(命令)并从不同的地方使用它。例如,您可以定义一个保存命令并同时使用一个菜单项、一个上下文菜单项和一个按钮来使用它。这样你就可以将任务集中起来。命令也支持数据绑定,这是 WPF 应用程序的一个非常强大的功能。据我所知,命令会导致某些类型的内存泄漏,但您可以通过使用许多变通方法来避免这种情况。我必须补充一点,MVVM 设计模式也使用命令作为设计 WPF 应用程序的标准方法。处理事件要简单得多,但命令提供了非常强大的设计。但是你现在必须知道你不能总是使用命令而不是事件。

回答by flq

Additionally WPF4.0 allows binding to command definitions. This makes it even easier to expose commands from your view models, utlimately helping you in separating logic from UI concerns.

此外,WPF4.0 允许绑定到命令定义。这使得从视图模型中公开命令变得更加容易,最终帮助您将逻辑与 UI 问题分开。

回答by knockNrod

Commands are routed events.

命令是路由事件。