Windows Phone 8.1 - MVVMLight - 为什么EventToCommand不起作用?

5

我无法在我的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"

我也尝试使用了[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>

我遇到了以下错误:
  • 成员 "Triggers" 未被识别或无法访问。
  • 在 XML 命名空间 'clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity' 中未知类型 'EventTrigger'
  • 错误 2:在类型 'Interaction' 中未找到可附加的属性 'Triggers'。...
请问有人可以帮我解决吗?
3个回答

4
你是瞄准 Silverlight 还是 WinRT(通用应用)类型的 Windows Phone 8.1?如果选择了第二个选项,在这篇 博客文章 中,MVVM Light 的作者解释了对 EventToCommand 的支持不足 - 基本上在 WinRT 中已经有类似于 EventToCommand 的机制 - 行为。

你知道关于第一个情况(针对Silverlight 8.1)的解决方案吗?我创建了一个新的WP 8.1项目,并通过NuGet添加了mvvm light,但是“GalaSoft.MvvmLight.Command”中的“EventToCommand”未找到。谢谢! - Dacian Mujdar

3

Windows Phone 8.1

Windows 8.1行为SDK:如何使用InputConverter调用InvokeAction传递参数到命令

微软开发了自己的EventToCommand功能。它位于Behaviors SDK中。有人在stackoverflow上说要通过Nuget获取此SDK。如果在NuGet中找不到该程序包,请在添加引用对话框中获取。

enter image description here (我的“添加引用”对话框可能与原始版本不同,因为使用了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>

1
升级我的WP8.0应用程序以使用MVVMLight 5.0+时禁用了EventToCommand行为,我的解决方案搜索引导我找到了博客文章, 提供源代码实现自定义命令操作,将事件参数作为参数传递。实现该代码后,我的应用程序恢复了正常工作!希望这可以帮到你:D

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接