MVVM开发模式MVVM Light Toolkit中使用事件和参数传递
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>
MVVM开发模式MVVM Light Toolkit中使用事件和参数传递相关推荐
- 玩转Android之MVVM开发模式实战,炫酷的DataBinding!
原文:http://blog.csdn.net/u012702547/article/details/52077515 ---------------------------------------- ...
- Android开发模式万佛朝中MVX(MVC、MVP、MVVM)
今天公司的测试服务器开小差了,后台被吐槽的体无完肤,虽然我们都知道跟他没有什么关系,但是生活需要乐趣,总要有人背锅,哈哈!~~~暂时没有环境开发了,那就分享一下之前做的一篇关于Android开发模式的 ...
- iOS OC mvvm开发模式
相信大家对MVC开发都已经不陌生了,最经典开发模式 MVC构成: M:model也就是数据模型 V:View视图 C:Controller控制器 Model和View是相互独立的.View只负责页面展 ...
- C#23种开发模式,陆续完善中
1 #region 单例模式 2 #region 线程非安全单例模式 3 public class Singleton1 4 { 5 private Singleton1() { } 6 privat ...
- wap开发问题总结(ios中click事件无法触发、身份证分隔、倒计时、验证码分隔等)
需求背景:用户在通过短信验证码方式注册或者登陆时,存在二次卡场景(运营商回收原机主不用的手机号,存放三个月后,再次开放给新机主),导致新机主可以登录原机主账号,如果原机主不换绑手机.二次卡判断条件:手 ...
- Android-MVVM开发模式应用
Android-MVVM开发模式应用 CSDN:https://blog.csdn.net/chenzhen200638/article/details/82185190 MVVM开发模式,是多种先进 ...
- 疫情下更合适的开发模式
问题的提出 任何复杂的软件都是团队工作的产物,所以我们会利用版本控制工具和不同的分支策略来协助团队的日常开发和交流,mainline开发模式和pull request开发模式(以下简称PR)则是最常用 ...
- Silverlight实用窍门系列:52.Silverlight中的MVVM框架极速入门(以MVVM Light Toolkit为例)...
在本文将以MVVM Light Toolkit为例讲解MVVM框架在现实中的使用入门,首先我们在http://mvvmlight.codeplex.com/下载它的MVVM框架下来.也可以通过 htt ...
- Mvvm Light Toolkit 入门
原文:Mvvm Light Toolkit 入门 前言 之前学习UWP的时候就一直看到有关MVVM的资料但是一直没有系统的去学,最近正好有时间,特地来攻破这个点,顺便学习一下VS与GitHub的链接和 ...
最新文章
- 禁止套娃!Redis官网宕机,返回“连接不上Redis”
- Appcelerator Titanium 3.x Win7 64位平台安装步骤
- HTML5增加的几个新的标签
- 算法之快速排序(递归和非递归)
- Seven times have I despised my soul 《我曾七次鄙视自己的灵魂》
- Maven项目中使用本地JAR包
- ZBrush软件特性之Color调控板
- win10下搭建QTP测试环境
- 【转载保存】Ubuntu14.04安装pycharm用于Python开发环境部署,并且支持pycharm使用中文输入
- Cannot load driver ‘C:\Keil_v5\ARM\Segger\JL2CM3.dll 报错解决方法。
- 手游和平精英透视教学
- android 那个桌面好,手机桌面软件哪个好?最好用的手机桌面软件推荐
- 信息系统的风险评估过程与评估方法
- 商贸宝显示连接不到服务器,登录T1商贸宝就提示 服务器链接失败 请重新登录 这个怎么解决?...
- python爬取守望先锋英雄图片以及描述
- python进行列联表卡方检验
- VB6-COM接口操作开源OpenOffice编辑Excel,Word文件(中国首创)
- MStar点屏(LVDS接口屏)
- Presto架构和使用总结
- 利用html2canvas和vue-qr生成带头像二维码的分享海报(二)
热门文章
- IBatis.Net学习笔记九--动态选择Dao的设计分析
- Javascript闭包和闭包的几种写法及用途
- jquery插件-表单验证插件-提示信息中文化与定制提示信息
- NeHe OpenGL第三十九课:物理模拟
- about ajax,About 4nf.org - Arvind Gupta | Ajaxify | The Ajax Plugin
- 一堆棋子java代码编程_网易2018校招内推编程题-堆棋子-C++实现
- ios集成firebase_如何将Firebase与您的应用程序集成
- 通过构建城市来解释HTML,CSS和JavaScript之间的关系
- Docker的使用(docker pull拉取镜像失败问题解决)
- 右键脚本html,js实现右键自定义菜单