WPF 命令与事件的优缺点 [英] WPF Commands vs Events Advantages/Disadvantages

查看:33
本文介绍了WPF 命令与事件的优缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

Commands provide two main benefits over event handlers:

  1. 命令没有链接到调用者,所以相同的命令不是依赖并且可以从菜单项、工具栏按钮、键盘、等
  2. 命令提供支持启用/禁用所有相关 UI基于状态的控制命令(可以执行或不执行)

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

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

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

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

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

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.

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

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

这篇关于WPF 命令与事件的优缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆