何时在 WPF 中使用事件而不是命令?

发布于 2024-11-09 00:35:23 字数 198 浏览 0 评论 0原文

您好,我最近研究了 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 技术交流群。

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

发布评论

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

评论(2

药祭#氼 2024-11-16 00:35:23

这是一个公平的问题,也是 MVVM 架构领域中一个常见但“已解决”(有争议的)问题。如果您使用 MVVM 框架,您可能会发现类似于 EventToCommand 行为的内容,此处是来自 MVVM Light Toolkit 的示例。

简而言之,这允许您将事件映射到命令绑定,如下所示:

<Rectangle Fill="White"
       Stroke="Black"
       Width="200"
       Height="100">
<i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseEnter">
        <cmd:EventToCommand Command="{Binding TestCommand,
                                      Mode=OneWay}"
           CommandParameter="{Binding Text,
                              ElementName=MyTextBox,
                              Mode=OneWay}"
           MustToggleIsEnabledValue="True" />
    </i:EventTrigger>
</i:Interaction.Triggers>
</Rectangle>

更新:

对于此问题还有另外两种“合理”的解决方案:

一种使用现在认为遗留的“AttachedCommandBehavior”扩展< a href="http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/" rel="noreferrer">此处。

其他有点刺激,但可行。

  1. 通过 en 捕获命令
    事件纯粹在视图中。
  2. 查询控件的DataSource 步骤
  3. 获取字符串绑定目标
    表示您的命令的标识符
    (也许在
    视图)
  4. 调用您的命令
    通过反射的视图模型
    传入命令参数。

这看起来很恶心,但我相当确定实际上比使用传统命令要快一些
绑定。为了确定我需要查看 IL,我认为在这种情况下这并不重要。

/更新

不过,我想指出,这并不总是理想的情况。我发现我经常使用 EventToCommand 来解决设计问题。请考虑以下事项:

  • 使用事件和代码隐藏来处理用户界面相关的行为。
  • 如果合适,请考虑创建具有命令绑定的自定义控件,特别是如果您发现自己使用命令来封装事件驱动的行为来设置随后反映在视图中的绑定数据。 (即根据与控件或类似内容的接近程度设置透明度值)
  • 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:

<Rectangle Fill="White"
       Stroke="Black"
       Width="200"
       Height="100">
<i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseEnter">
        <cmd:EventToCommand Command="{Binding TestCommand,
                                      Mode=OneWay}"
           CommandParameter="{Binding Text,
                              ElementName=MyTextBox,
                              Mode=OneWay}"
           MustToggleIsEnabledValue="True" />
    </i:EventTrigger>
</i:Interaction.Triggers>
</Rectangle>

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.

  1. Capture a command via en
    event purely in the view.
  2. Query the control's DataSource Step
  3. Grab a string binding target
    identifier that denotes your command
    (perhaps using a const string on the
    view)
  4. Invoke your command on
    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:

  • Use events and code behind to handle User-Interface related behaviors.
  • Consider creating custom controls that have command bindings if appropriate, especially if you find yourself using commands to encapsulate event driven bevahior to set bound data that is then reflected in the view. (i.e. setting a transparency value based on proximity to a control or something similar)
  • EventToCommand should most likely be used to handle "Command-like" events only (double clicking etc) not reactive events (mouse-over). However there is nothing preventing this. Implement as you see fit.

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.

扭转时空 2024-11-16 00:35:23

您可能想看看这篇文章:

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).

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