MVVM光:添加EventToCommand在XAML没有混合,更简单的方式或片段



谁能告诉我EventToCommand类的实际语法是什么?从我所相信的是EventToCommand类与Silverlight/WPF和WP7一起工作,因此我认为它是一个更好的选择。

从我所相信的,我可以添加任何点击事件,并得到它强制进入我的ViewModel,但我有一个问题,找到最好的方法来做到这一点。

我知道你可以添加它没有混合,但有片段可用吗?

或者有一个更简单的方法来添加它通过VS 2010?任何帮助,或者如果有人知道一个好的教程,这将是伟大的。

假设您使用.NetFramework4:

首先添加namespace:

xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"

加载事件的语法。

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <cmd:EventToCommand Command="{Binding Mode=OneWay, Path=LoadedCommand}"
                            PassEventArgsToCommand="True" />
    </i:EventTrigger>
</i:Interaction.Triggers>

我更新了我的项目,看起来他们把命令移到了:

xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"

0)如果你不知道WPF和MVVM,那么阅读Josh Smith关于WPF和MVVM模式的文章https://msdn.microsoft.com/en-us/magazine/dd419663.aspx

1)在你的项目中添加包(通过NuGet) MvvmLightLibs

2)添加引用到System.Windows.Interactivity

3)在"View"XAML中添加:

)

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:command="http://www.galasoft.ch/mvvmlight"

b)

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Closing">
      <command:EventToCommand Command="{Binding OnClosingCommand}"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</Window>
4)在ViewModel中添加必要的属性
public ICommand OnClosingCommand
{
  get
  {
    return new RelayCommand(() => SomeMethod());
  }
}

注:在你的视图中应该指定DataContext (XAML)

  <Window.DataContext>
    <vm:MainWindowViewModel/>
  </Window.DataContext>

这是工作。

最新更新