何时在 WPF 中使用事件而不是命令?
您好,我最近研究了 WPF 并开始学习事件和命令。我通常在单击按钮时使用命令,这会导致方法在我的“视图模型”中运行。
是否可以通过使用命令使 Button 对任何其他事件(例如 MouseOver 事件)做出反应?或者在这种情况下会使用 WPF 事件吗?
如果要使用 WPF 事件,那么事件处理程序实现是否应该仅调用视图模型中的方法来保持关注点分离?
Hi i have recently looked into WPF and started learning about Events and Commands. I typically use Commands on Button clicks which causes a method to Run in my "view model".
Is it possible to make the Button react to any other events like the MouseOver event through the use of commnds? Or would WPF Events be used in this case?
If WPF Events are to be used, then should the event handler implementation just call a method in the View Model to keep concerns sperate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个公平的问题,也是 MVVM 架构领域中一个常见但“已解决”(有争议的)问题。如果您使用 MVVM 框架,您可能会发现类似于 EventToCommand 行为的内容,此处是来自 MVVM Light Toolkit 的示例。
简而言之,这允许您将事件映射到命令绑定,如下所示:
更新:
对于此问题还有另外两种“合理”的解决方案:
一种使用现在认为遗留的“AttachedCommandBehavior”扩展< a href="http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/" rel="noreferrer">此处。
其他有点刺激,但可行。
事件纯粹在视图中。
表示您的命令的标识符
(也许在
视图)
通过反射的视图模型
传入命令参数。
这看起来很恶心,但我相当确定实际上比使用传统命令要快一些
绑定。为了确定我需要查看 IL,我认为在这种情况下这并不重要。
/更新
不过,我想指出,这并不总是理想的情况。我发现我经常使用 EventToCommand 来解决设计问题。请考虑以下事项:
最重要的也许是您记得您是开发人员。指导方针本身并不能解决问题,但考虑指导方针可能会使问题的解决方案变得显而易见。
This is a fair question, and one that is a common, yet "solved" (debatably) problem within the MVVM architecture realm. If you are using an MVVM framework, you are likely to find something similar to the EventToCommand Behavior, here is the sample from the MVVM Light Toolkit.
In short, this allows you to map an event to a command binding like so:
Update:
There are two other "reasonable" solutions to this problem:
One uses the now considered legacy "AttachedCommandBehavior" extension found here.
The other is a little bit irritating, but workable.
event purely in the view.
identifier that denotes your command
(perhaps using a const string on the
view)
the view model via reflection and
pass in the command arguments.
This looks gross but I'm fairly certain is actually a bit faster than just using traditional command
bindings. In order to be sure I'd need to see the IL, and I don't think that it matters in this case.
/Update
I want to note however that this is not always an ideal situation. I've discovered that more often than not, I'm using EventToCommand to cover a design issue. Please consider the following:
Most importantly perhaps is that you remember that you are the developer. Guidelines in themselves do not solve problems, but consideration of guidelines may make the solution to a problem apparent.
您可能想看看这篇文章:
WPF 命令与事件的优点/缺点
讨论了事件和命令的不同用法。
至于其他事件的命令,您应该看看类似 EventToCommand 作为MVVMLight 工具包的一部分a>,它允许您将任何事件附加到视图模型中的命令。非常有用,特别是如果您已经在使用 MVVM Light(我强烈推荐)。
You might want to take a look at this post:
WPF Commands vs Events Advantages/Disadvantages
which talks about the different usages of events and commands.
As far as commands for other events, you should take a look at something like EventToCommand as part of the MVVMLight Toolkit, which allows you to attach any event to a command in your viewmodel. Quite useful, especially if you're already using MVVM Light (which I highly recommend).