MVVM开发模式MVVM Light Toolkit中使用事件和参数传递

news/2024/7/7 23:18:03

Light中定义了类GalaSoft.MvvmLight.Command.RelayCommand

这个类继承了ICommand方法,实现了其中的方法,Action就是一个方法参数

复制代码
// 摘要:     //     A command whose sole purpose is to relay its functionality to other objects     //     by invoking delegates. The default return value for the CanExecute method     //     is 'true'. This class does not allow you to accept command parameters in     //     the Execute and CanExecute callback methods.     public class RelayCommand : ICommand     {         // 摘要:         //     Initializes a new instance of the RelayCommand class that can always execute.          public RelayCommand(Action execute);         //         // 摘要:         //     Initializes a new instance of the RelayCommand class.          public RelayCommand(Action execute, Func<bool> canExecute);          // 摘要:         //     Occurs when changes occur that affect whether the command should execute.         public event EventHandler CanExecuteChanged;          // 摘要:         //     Defines the method that determines whether the command can execute in its         //     current state.          [DebuggerStepThrough]         public bool CanExecute(object parameter);         //         // 摘要:         //     Defines the method to be called when the command is invoked.          public void RaiseCanExecuteChanged();     }
复制代码

做一个简单的导航事件

从MainView导航到View1,

在View1的Viewmodel中定义这个事件属性

 public RelayCommand GotoView1
        {
            get;
            set;
        }

在构造函数中赋值给GotoView1

 GotoView1 = new RelayCommand(delegate {
                System.Windows.MessageBox.Show("Go to View1");
            });

//或者定义一个单独的方法  GotoView1 = new RelayCommand( ()=>GoToView1());

然后在MainPage.xaml中绑定事件

  <HyperlinkButton x:Name="View1" Grid.Row="1" Content="Go to View1" Command="{Binding GotoView1}" ></HyperlinkButton>

 

参数传递。

定义事件的时候很多情况下都需要传入参数,MVVM light也有这功能,而且 比较简单,方式如下:

//<string>这个就是GotoView1方法需要的参数,参数类型是string,当然可以根据需要变成其他类型,实体类型都可以
复制代码

public RelayCommand<string> GotoView1 { get; set; }

  public void GoToView11(string msg)
  {
         System.Windows.MessageBox.Show(msg + "Go to View1");
  }

//对这个方法属性赋值,test只是形式参数,无任何意义,意思是把字符串类型的test传递给GotoView方法
GotoView1 = new RelayCommand<string>((test) => GoToView11(test));


复制代码

页面上绑定这个方法的时候可以指定CommandParameter来指定Command的绑定方法的参数,如果类型是字符串,直接="字符串"就可以

如果是其他实体类型可以参考使用Binding语法CommandParameter="{Binding SelectedItem, ElementName=集合控件}"

            <HyperlinkButton x:Name="View1" Grid.Row="1" Content="Go to View1" Command="{Binding GotoView1}" CommandParameter="Hello" ></HyperlinkButton>

 


http://lihuaxi.xjx100.cn/news/268333.html

相关文章

上海松江大学城学计算机,上海松江大学城有几所学校?

就上大学网为您推荐类似问题答案问题1&#xff1a;上海金融系比较好的大学有哪几所&#xff1f;这几所学校2015年高考录取分数线又是多少&#xff1f;上海金融系比较好的大学有不少了&#xff0c;比如&#xff1a;复旦大学(526)&#xff0c;上海交通大学(512)&#xff0c;同济大…

python fun

import turtle t turtle.Pen() for i in range(360):t.forward(i)t.left(59)

mysql插入大量数据

创建实验表&#xff1a; CREATE TABLE a ( id int(11) NOT NULL AUTO_INCREMENT, name char(50) NOT NULL, type char(20) NOT NULL, PRIMARY KEY (id)) ENGINEInnoDB&#xff1b; 创建存储语句&#xff1a; delimiter // create procedure insertdata() begin declare i int …

谷歌顶级量子科学家详述他为何从谷歌辞职

加州大学圣塔芭芭拉分校&#xff08;UCSB&#xff09;的教授John Martinis作者 | Paul Smith-Goodson译者 | 天道酬勤&#xff0c;责编 | Carol 约翰马丁尼斯&#xff08;John Martinis&#xff09;教授从Google辞职的消息在整个量子学界引起了轩然大波。消息宣布几天后&#x…

Linux之切换目录命令cd

1. 切换目录命令的使用 命令说明cd 目录切换到指定目录cd ~ 【与直接cd效果是一样的】切换到当前用户的主目录cd ..切换到上一级目录cd .切换到当前目录cd -切换到上一次目录 注意: cd (change directory)cd命令切换目录时&#xff0c;这个目录必须存在。cd 后面不写目录等…

一行命令搞定图像质量评价 | 附代码和操作步骤

点击上方“小白学视觉”&#xff0c;选择加"星标"或“置顶”重磅干货&#xff0c;第一时间送达在交流群里&#xff0c;经常有人问到图像质量评价的问题。比如对监控摄像头拍摄的多幅图像&#xff0c;挑选一幅图像显示给用户&#xff0c;或者选择一幅图丢给识别模型&a…

ddr2的说明

这里是对华邦ddr2的型号理解 W9751GG6KB-25 这个是ddr2 内存为1Gbit W9751G6KB-25 这个是ddr2 内存为512Mbit

转:YUV RGB 常见视频格式解析

转&#xff1a; http://www.cnblogs.com/qinjunni/archive/2012/02/23/2364446.html YUV RGB 常见视频格式解析 I420是YUV格式的一种&#xff0c;而YUV有packed format和planar format两种&#xff0c;而I420属于planar format的一种。  同时I420表示了YUV的采样比例4:2:0。4…