【问题标题】:Windows Phone 8.1 - MVVMLight - Why is EventToCommad not working?Windows Phone 8.1 - MVVMLight - 为什么 EventToCommad 不工作?
【发布时间】:2014-06-15 03:43:40
【问题描述】:

我无法在我的 Windows Phone 8.1 应用程序中使用 EventToCommand。

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8"

我也尝试了 assembly=GalaSoft.MvvmLight.Extras.WP81...

<controls:PivotItem Name="pivotItem">
<i:Interaction.Triggers>
    <i:EventTrigger EventName="SelectionChanged">
        <cmd:EventToCommand Command="{Binding SelectServiceCommand}"
                            CommandParameter="{Binding SelectedIndex,                                 ElementName=pivotItem}"/>
    </i:EventTrigger>
    <!-- other stuff  -->
</i:Interaction.Triggers>

我得到以下错误:

  • “触发器”成员无法识别或无法访问。
  • XML 命名空间“clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity”中的未知类型“EventTrigger”
  • 错误 2 在类型“交互”中找不到可附加属性“触发器”。 ...

有人可以帮帮我吗?

【问题讨论】:

    标签: windows-phone-8 mvvm-light windows-phone-8.1 eventtocommand


    【解决方案1】:

    您的目标是 Silverlight 还是 WinRT(通用应用程序)类型的 Windows Phone 8.1?

    如果您选择了第二个选项,那么在blog postMVVM Light 作者解释了缺乏对 EventToCommand 的支持 - 基本上在 WinRT 中已经有一个类似于 EventToCommand 的机制 - 行为。

    【讨论】:

    • 您知道第一种情况的解决方案(针对 Silverlight 8.1)吗?我创建了一个新的 WP 8.1 项目并通过 NuGet 添加了 mvvm 光槽,但未找到来自“GalaSoft.MvvmLight.Command”的“EventToCommand”。谢谢!
    【解决方案2】:

    Windows Phone 8.1

    Windows 8.1 Behavior SDK: How to use InvokeAction with InputConverter to pass arguments to a Command

    Microsoft 开发了自己的 EventToCommand 功能。它位于行为 SDK 中。 stackoverflow 上有人告诉通过 Nuget 获取此 SDK。如果您在 NuGet 中找不到包 - 请在 Add reference dialog 中获取。

    (由于Productivity Power Tools 扩展,我的“添加参考”对话框可能与原始对话框不同)

    以下是简单用法示例:

    <ListBox ItemsSource="{Binding Persons, Mode=OneWay}" 
             SelectedItem="{Binding SelectedPerson, Mode=TwoWay}">
        <interactivity:Interaction.Behaviors>
            <core:EventTriggerBehavior EventName="SelectionChanged">
                <core:InvokeCommandAction Command="{Binding DisplayPersonCommand}" />
            </core:EventTriggerBehavior>
        </interactivity:Interaction.Behaviors>
    </ListBox>
    

    【讨论】:

      【解决方案3】:

      当升级我的 WP8.0 应用程序以使用 MVVMLight 5.0+ 禁用 EventToCommand 行为时,我对解决方案的搜索将我带到了这个blog post,它提供了用于实现自定义命令操作的源代码,该操作将事件参数作为范围。实施代码使我的应用恢复正常工作!!

      希望对你有所帮助:D

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-28
        • 2014-10-20
        • 2014-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多