
mvvm模式里command经常写在vm中。而command只能传入一个对象作为执行时的参数,若要传入多个参数,在.cs文件(即调用vm的command)中,只需要把多个参数加到一个集合里,传入command时就把集合当单参数对象传入就行了。
如:
但如果在xaml中用到如blend的InvokeCommandAction进行command的绑定,又如何在xaml中进行传入多参数??
方法有许多种。小弟不才,自己开发了2个类来解决这问题。先说明一下,此方法只使用与silverlight4或以上版本。
先看看应用:
关于DelegateCommand的实现:
using System;using System.windows.input;namespace System.windows.input{ public class DelegateCommand<T> : ICommand { public DelegateCommand() : this(null,null) { } public DelegateCommand(Action<T> executeMethod) : this(executeMethod,null) { } public DelegateCommand(Action<T> executeMethod,Func<T,bool> canExecuteMethod) { TargetExecuteMethod = executeMethod; TargetCanExecuteMethod = canExecuteMethod; } public Action<T> TargetExecuteMethod { get; set; } public Func<T,bool> TargetCanExecuteMethod { get; set; } public voID OnCanExecuteChanged() { this.CanExecuteChanged(this,EventArgs.Empty); } public voID Execute(T parameter) { if (TargetExecuteMethod != null) TargetExecuteMethod(parameter); } public bool CanExecute(T parameter) { if (TargetCanExecuteMethod != null) return TargetCanExecuteMethod(parameter); if (TargetExecuteMethod != null) return true; return false; } #region ICommand bool ICommand.CanExecute(object parameter) { return this.CanExecute((T)parameter); } voID ICommand.Execute(object parameter) { this.Execute((T)parameter); } public event EventHandler CanExecuteChanged; #endregion } }
欢迎各大网友来吐槽。
下载地址:http://download.csdn.net/source/2979146
总结以上是内存溢出为你收集整理的mvvm---如何在xaml里,把多个参数传入到command全部内容,希望文章能够帮你解决mvvm---如何在xaml里,把多个参数传入到command所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)