【问题标题】:How to use Composite command in mvvm如何在 mvvm 中使用 Composite 命令
【发布时间】:2016-07-28 09:09:00
【问题描述】:

简介 我正在使用带有 MVVM-Light 应用程序的 WPF

目标

我必须从同一个事件中调用两个命令,这可以使用 MVVM 吗?

Xaml 看起来像这样

 <i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <command:EventToCommand Command="{Binding Command1}" PassEventArgsToCommand="False" />
        <command:EventToCommand Command="{Binding Command2}" PassEventArgsToCommand="False" />
    </i:EventTrigger>
</i:Interaction.Triggers>

问题

当挂钩两个命令时,触发事件时仅调用其中一个。

Q1 如何在一个事件中调用两个命令?

我听说过Composite commands in PRISM

例如,CompositeCommand 类在 Stock Trader Reference 实现(股票交易者 RI)中用于实现由买入/卖出视图中的“全部提交”按钮表示的 SubmitAllOrders 命令。 当用户单击“全部提交”按钮时,将执行由单个买卖交易定义的每个 SubmitCommand

.
Q2 MVVM 中有这样的东西吗?

【问题讨论】:

  • 对于问题 2:可能您所能做的就是单独定义 CompositeCommand 并将 Commands 数组作为 CommandParameter 传递给它。然后在 RelayCommand 中获取参数并分别调用每个传递的命令。
  • 是的,那个铃响了

标签: c# wpf xaml mvvm prism


【解决方案1】:

尝试使用 2 个事件触发器:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <command:EventToCommand Command="{Binding Command1}" PassEventArgsToCommand="False" />
    </i:EventTrigger>
    <i:EventTrigger EventName="Loaded">
        <command:EventToCommand Command="{Binding Command2}" PassEventArgsToCommand="False" />
    </i:EventTrigger>
</i:Interaction.Triggers>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    相关资源
    最近更新 更多