【问题标题】:MvvmLight EventToCommand not working anymoreMvvmLight EventToCommand 不再工作
【发布时间】:2013-11-09 18:11:23
【问题描述】:

我在 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”

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

【问题讨论】:

    标签: c# xaml mvvm windows-phone-8 mvvm-light


    【解决方案1】:

    这些命名空间是正确的。

    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)
    {
    }
    

    【讨论】:

      【解决方案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>
      

      【讨论】:

      • 我的问题已经很老了,我什至不记得这个问题的解决方案,但是是的,我们目前使用微软的 EventToCommand 功能,所以我想我可以将你的答案标记为正确。跨度>
      猜你喜欢
      • 1970-01-01
      • 2016-06-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-30
      相关资源
      最近更新 更多