silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了mvvm---如何在xaml里,把多个参数传入到command大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_616_2@

概述

     mvvm模式里command经常写在vm中。而command只能传入一个对象作为执行时的参数,若要传入多个参数,在.cs文件(即调用vm的command)中,只需要把多个参数加到一个集合里,传入command时就把集合当单参数对象传入就行了。      如: public ICommand CMD { get
@H_616_2@
@H_616_2@ @H_616_2@
@H_616_2@

     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

@H_616_2@
@H_616_2@
@H_616_2@@H_616_2@

大佬总结

以上是大佬教程为你收集整理的mvvm---如何在xaml里,把多个参数传入到command全部内容,希望文章能够帮你解决mvvm---如何在xaml里,把多个参数传入到command所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。