Windwos Phone MVVM Light的 EventToCommand 命令绑定实现方式

来源:互联网 发布:鸳鸯 知乎 编辑:程序博客网 时间:2024/05/22 13:36
1.用户点击一个按钮时需要执行一个方法,这时可以使用mvvm light的EventToCommand来实现.

2.如果需要实现点击ListBox中的一项,显示详情页面,这时需要向详情页面传递一个参数,也可以用EventToCommand来实现.

下面我将演示这两个情情况的具体使用方法:

第一种,点击一个按钮,执行点击事件

(1)使用MvvmLight的EventToCommand事件需要引用两个命名空间分别为:

  xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"  
  (该命名空间需要引用System.Windows.Interactivity.dll)  xmlns:command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP71"

(2)在MainPage.xaml页面上添加一个按钮,并引用EvntToCommand事件

  <Button Content="TestEventToCommand"          VerticalAlignment="Center">          <i:Interaction.Triggers>             <i:EventTrigger EventName="Click">                 <command:EventToCommand Command="{Binding ButtonClickCommand}"/>             </i:EventTrigger>         </i:Interaction.Triggers>   </Button>

(3)在MainPageViewModel里面添加RelayCommand属性并在构造函数中初始化      

  public MainViewModel()  {        ButtonClickCommand = new RelayCommand(OnButtonClick);  }   public RelayCommand ButtonClickCommand { get; set; }   //按钮的回调方法   private void OnButtonClick()   {        //当按钮被按下时需要执行的逻辑   }

 2.使用带参数的EventToCommand向调用函数传递参数,有两种实现方式.下面分别进行介绍.

(1)首选在MainPage.xaml中定义一个ListBox

 <ListBox ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding ItemSource}">          <i:Interaction.Triggers>               <i:EventTrigger EventName="SelectionChanged">                     <command:EventToCommand Command="{Binding ListBoxSelectionChangedCommand}"  PassEventArgsToCommand="True"/> (一旦设置了PassEventArgsToCommand=True,就会把SelectionChanged事件的EventArg参数传递给绑定方法,非常好用)             </i:EventTrigger>          </i:Interaction.Triggers>  </ListBox>


<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> //ListBox的ItemTemplate定义</span>
<phone:PhoneApplicationPage.Resources>  <DataTemplate x:Key="ItemTemplate">       <StackPanel Margin="{StaticResource PhoneTouchTargetOverhang}">                <TextBlock Text="{Binding Name}"/>             <TextBlock Text="{Binding Sex}"/>       </StackPanel>  </DataTemplate> </phone:PhoneApplicationPage.Resources>

(2)然后在MainPage.xaml中定义事件属性和实现,并在构造函数中初始化.

 public MainViewModel() {     ListBoxSelectionChangedCommand= new RelayCommand(OnListBoxSelecgtionChanged); }
 public RelayCommand<SelectionChangedEventArgs> ListBoxSelectionChangedCommand { get; set; } //按钮的回调方法 private void OnListBoxSelecgtionChangedSelectionChangedEventArgs e) {       //Cast 方法需要引入System.linq命名空间后才能使用       Item item = e.AddedItems.Cast<Item>().First(); }

第二种实现方式

(1)对ListBox进行命名,然后使用CommandParameter参数(注意这种实现方式和第一种的区别)

<ListBox Name="lb" ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding ItemSource}">          <i:Interaction.Triggers>               <i:EventTrigger EventName="Tap">                   <command:EventToCommand Command="{Binding ListBoxSelectionChangedCommand}"                                           CommandParameter="{Binding Path=SelectedItem, ElementName=lb}"/>                </i:EventTrigger>          </i:Interaction.Triggers></ListBox>

(2)MainPageVewiModel.cs    

public RelayCommand<Item> ListBoxSelectionChangedCommand { get; set; }        //按钮的回调方法private void OnListBoxSelecgtionChanged(Item e){}

我的Item定义如下   

public class Item{      public string Name { get; set; }      public string Sex { get; set; }}


 

   

 

原创粉丝点击