WPF 命令和事件有什么区别?

发布于 2024-09-24 03:01:28 字数 61 浏览 0 评论 0原文

WPF CommandEvent 之间有什么区别?

What is the difference between WPF Command and Event?

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

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

发布评论

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

评论(5

许仙没带伞 2024-10-01 03:01:28

一般来说,您可以对事件执行与命令几乎相同的操作,只是处理用户交互的模式不同。

WPF 中的命令允许您将命令处理程序的实现移至业务层。命令结合了启用状态和执行,因此一切就绪。通过搜索 MVVM 模式来阅读更多内容。

命令一开始实现起来比较复杂,因此如果您的应用程序很小,您应该考虑只使用事件。

Generally speaking you can do almost the same with events as with commands, it is just a different pattern of handling user interaction.

Commands in WPF allow you to move the implementation of a command handler to the buisness layer. Commands combine Enable state and executation, so everything is in place. Reade more by searching for the MVVM pattern.

Commands are more complex to implement at first, so if your application is small you should consider just sticking to events.

紫南 2024-10-01 03:01:28

命令与事件类似,只是我们可以将任意数量的 UI 控件或输入手势与命令相关联,并将该命令绑定到在激活控件或手势时执行的处理程序被执行。

Command 还可以跟踪天气是否可用。如果它们不可用,则与该命令关联的所有控件都将被禁用。

调用命令时执行的代码位于命令 Execute 事件处理程序中。
确定命令是否可以被调用的代码位于命令CanExecute 事件处理程序中。

WPF 有一些内置命令:

Command Class          | Example Commands
-----------------------------------------------
ApplicationCommands    | Close, Cut, Copy, Paste, Save, Print
NavigationCommands     | BrowseForward, BrowseBack, Zoom, Search
EditingCommands        | AlignXXX, MoveXXX, SelectXXX
MediaCommands          | Play, Pause, NextTrack, IncreaseVolume, Record, Stop

Commands are similar to Events except we can associate any number of UI Controls or Input Gestures to a command and bind that command to a handler that is executed when control are activated or gestures are performed.

Command also keep track weather or not they are available. If they are not available then all controls associated with that command are disabled.

The Code that executes when command is invoked is located in commands Execute event handler.
The Code that determines is command can be or can not be invoked is located in commands CanExecute event handler.

WPF have some inbuilt Commands:

Command Class          | Example Commands
-----------------------------------------------
ApplicationCommands    | Close, Cut, Copy, Paste, Save, Print
NavigationCommands     | BrowseForward, BrowseBack, Zoom, Search
EditingCommands        | AlignXXX, MoveXXX, SelectXXX
MediaCommands          | Play, Pause, NextTrack, IncreaseVolume, Record, Stop
朮生 2024-10-01 03:01:28

您可以在视图(XAML)中绑定WPF命令并接收引发的事件。这样您就不必使用 MVVM 中禁止的后台代码。

所以绑定元素非常重要。但它也实现了CanExecute,如果它返回 false(例如,如果它是一个按钮),通常会使您的控件被禁用。

You can bind WPF command in the view (XAML) and receive the event raised. This way you do not have to use code behind which is a no-no in MVVM.

So the binding element is very important. But it also implements CanExecute which normally makes your control disabled if it returns false, e.g. if it is a button.

奈何桥上唱咆哮 2024-10-01 03:01:28

在事件中,一个动作与其源紧密耦合,并且不能自由地重用;使用命令,您可以轻松地在一个位置维护各种操作,并在应用程序中的任何地方重用它们。

命令与命令有何不同
附加到一个简单的事件处理程序
按钮或计时器是命令
将语义和
动作的发起者来自其逻辑。这允许多个不同的源调用相同的命令逻辑,并且允许为不同的目标定制命令逻辑。

摘自 - 命令概述:http://msdn .microsoft.com/en-us/library/ms752308(v=VS.90).aspx

本文解释了命令的概念,在使用命令之前必须阅读。

这个线程也有很多有用的信息。 :

自定义 WPF 命令模式示例

In events an action is tightly coupled with its source and can't be reused freely; Using commands you can easily maintain various actions in a single place and reuse them anywhere in application.

What makes commands different from a
simple event handler attached to a
button or a timer is that commands
separate the semantics and the
originator of an action from its logic. This allows for multiple and disparate sources to invoke the same command logic, and it allows the command logic to be customized for different targets.

taken from - Commanding Overview: http://msdn.microsoft.com/en-us/library/ms752308(v=VS.90).aspx

This article explains the concept of Commands and is must read before using commands.

This SO thread is also havign a lot of useful info. :

Custom WPF command pattern example

关于从前 2024-10-01 03:01:28

粗略地说,Command是对Object(按钮、菜单)启用/禁用状态和操作的封装。

命令的限制:

  • 多播(可以使用多重绑定,但不常见,而且感觉不像事件多播那么好)
  • 需要两步才能点亮灯泡。 (如果您的按钮始终启用,那么您只是浪费代码)。

Roughly speaking, Command is encapsulation of Object (Button, Menu) Enable/Disable State and Action.

Limitation of Command:

  • Multicast (you can use multi binding but not so common and don't feel as nice as event multicast)
  • Need 2 step to light up the bulb. (If your button is always enable, then you just waste your code).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文