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; }}
- Windwos Phone MVVM Light的 EventToCommand 命令绑定实现方式
- MVVM Light中EventToCommand的使用
- Windows Phone 7: MVVM之命令绑定
- MVVM实现任意命令的绑定方法
- WPF与MVVM的实现(四)命令绑定
- MVVM-Light模式,在dataGrid的模板下,绑定事件不触发的原因已经服务端排序的实现
- Windows Phone 7 MVVM模式通讯方式之实现Command
- Windows Phone 7 MVVM模式通讯方式之实现Command
- Windows Phone 7 MVVM模式通讯方式之实现Command
- MVVM数据绑定的实现方式(KVO、block、Delegate、Notification、RAC)
- MVVM Light Messenger的使用
- windows phone:Navigation in a #WP7 application with MVVM Light
- Windows Phone 系列- Simple MVVM Navigation 页面导航的实现
- MVVM Light
- 采用MVVM方式实现WPF的TreeView
- Mvvm Light Toolkit for wpf/silverlight系列之数据绑定
- Mvvm Light Toolkit for wpf/silverlight系列之数据绑定
- Windows Phone 7 MVVM模式通讯方式之实现Attached Behavior
- uva 10118 - Free Candies
- Cocos2d-x 用C++ API File类读取文件
- Log4j使用总结
- 企业创新:先发射子弹 再发射炮弹
- JButton增加图标
- Windwos Phone MVVM Light的 EventToCommand 命令绑定实现方式
- HDU 2546 饭卡
- android多国语言---简写对应国家
- 百年中国的思想脑震荡
- ExecuteScalar方法
- 电子商务安全有要求
- php有关函数
- js 验证身份证号码
- Python tuple 元组详解