2

我在 Windows Phone 8 项目中使用 GalaSoft - MVVM Light Toolkit 遇到了一个相当奇怪的问题。突然(在合并了一些东西之后)我所有的 EventToCommand 调用都不再工作了。他们以前工作过。我已经尝试删除 MvvmLight 工具包并使用 Nuget 再次引用它,但结果保持不变。

一个例子:

MainMenuPage.xaml

<phone:PhoneApplicationPage 
       ...
       xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
       xmlns:commands="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8"
       ....>

 <phone:PhoneApplicationPage.DataContext >  
    <Binding Source="{StaticResource MainMenuViewModel}"/>
 </phone:PhoneApplicationPage.DataContext>


 <!-- catch back key press -->
 <i:Interaction.Triggers>
    <i:EventTrigger EventName="BackKeyPress">
        <commands:EventToCommand 
            Command="{Binding BackKeyPressCommand}"
            PassEventArgsToCommand="True"/>
    </i:EventTrigger>
 </i:Interaction.Triggers>

MainMenuViewModel.cs

 // back key press command
 public RelayCommand<object> BackKeyPressCommand { get; set; }

 public MainMenuViewModel()
 {
       BackKeyPressCommand = new RelayCommand<object>(
           BackKeyPress,
           (o) => true
           );

       ...
  }

  private void BackKeyPress(Object o)
  {
        // handle back key press
  }

这在以前非常有效,但现在 BackKeyPress(Object o) 方法不再被调用。所有 EventToComamnd 调用都会发生这种情况。

如果我删除 xmlns 标签,Resharper 建议添加:

xmlns:command="http://www.galasoft.ch/mvvmlight"

结果:

名称空间“ http://www.galasoft.ch/mvvmlight ”中不存在名称“EventToCommand ”

任何人都遇到过类似的问题或知道是什么原因造成的?

4

2 回答 2

2

这些命名空间是正确的。

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

但是您的 PhoneApplicationPage 的 DataContext 错误。

DataContext="{Binding Path=MainMenuViewModel, Source={StaticResource Locator}}"

MainMenuViewModel 是 ViewModelLocator 中的属性:

public MainMenuViewModel MainMenuViewModel
{
    get
    {
        return ServiceLocator.Current.GetInstance<MainMenuViewModel>();
    }
}

顺便说一句,BackKeyPress 的参数是 CancelEventArgs。你应该使用:

public RelayCommand<CancelEventArgs> BackKeyPressCommand { get; private set; }

this.BackKeyPressCommand = new RelayCommand<CancelEventArgs>(this.BackKeyPress)

private void BackKeyPress(CancelEventArgs e)
{
}
于 2013-10-30T10:04:20.187 回答
1

视窗电话 8.1

Windows 8.1 行为 SDK:如何使用 InvokeAction 和 InputConverter 将参数传递给命令

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>
于 2014-12-22T10:21:15.863 回答