DelegateCommand.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace SC
{
    /// <summary>
    /// delegate command for view model
    /// </summary>
    public class DelegateCommand : ICommand
    {
        #region members
        /// <summary>
        /// can execute function
        /// </summary>
        private readonly Func<bool> canExecute;

        /// <summary>
        /// execute function
        /// </summary>
        private readonly Action execute;

        #endregion

        /// <summary>
        /// Initializes a new instance of the DelegateCommand class.
        /// </summary>
        /// <param name="execute">indicate an execute function</param>
        public DelegateCommand(Action execute)
            : this(execute, null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the DelegateCommand class.
        /// </summary>
        /// <param name="execute">execute function </param>
        /// <param name="canExecute">can execute function</param>
        public DelegateCommand(Action execute, Func<bool> canExecute)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }

        /// <summary>
        /// can executes event handler
        /// </summary>
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        /// <summary>
        /// implement of icommand can execute method
        /// </summary>
        /// <param name="parameter">parameter by default of icomand interface</param>
        /// <returns>can execute or not</returns>
        public bool CanExecute(object parameter)
        {
            if (this.canExecute == null)
            {
                return true;
            }

            return this.canExecute();
        }

        /// <summary>
        /// implement of icommand interface execute method
        /// </summary>
        /// <param name="parameter">parameter by default of icomand interface</param>
        public void Execute(object parameter)
        {
            this.execute();
        }
    }

    /// <summary>
    /// delegate command for view model
    /// </summary>
    public class DelegateCommand<T> : ICommand
    {
        #region members
        /// <summary>
        /// can execute function
        /// </summary>
        private readonly Func<T, bool> canExecute;

        /// <summary>
        /// execute function
        /// </summary>
        private readonly Action<T> execute;

        #endregion

        /// <summary>
        /// Initializes a new instance of the DelegateCommand class.
        /// </summary>
        /// <param name="execute">indicate an execute function</param>
        public DelegateCommand(Action<T> execute)
            : this(execute, null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the DelegateCommand class.
        /// </summary>
        /// <param name="execute">execute function </param>
        /// <param name="canExecute">can execute function</param>
        public DelegateCommand(Action<T> execute, Func<T, bool> canExecute)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }

        /// <summary>
        /// can executes event handler
        /// </summary>
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        /// <summary>
        /// implement of icommand can execute method
        /// </summary>
        /// <param name="parameter">parameter by default of icomand interface</param>
        /// <returns>can execute or not</returns>
        public bool CanExecute(object parameter)
        {
            if (this.canExecute == null)
            {
                return true;
            }

            return this.canExecute((T)parameter);
        }

        /// <summary>
        /// implement of icommand interface execute method
        /// </summary>
        /// <param name="parameter">parameter by default of icomand interface</param>
        public void Execute(object parameter)
        {
            this.execute((T)parameter);
        }
    }
}

时间: 2024-08-04 05:28:02

DelegateCommand.cs的相关文章

WPF比较两个随机数大小写,利用MVVM思想实现

MVVM模式是把表现层和业务层完全分离,所以这里就使用MVVM制作一个极其简单的WPF的例子: 先看看最终图: 上图,需要实现的是,界面两个按钮,一个是生成随机两个数,一个是对两个数比较,把大的数显示出来.所以需要三个属性,两个事件. 由于逻辑比较简单,不用写model等类,实现如下: 1.创建基类NotifictionObject.cs,实现接口INotifyPropertyChanged class NotifictionObject : INotifyPropertyChanged { p

WPF MVVM+EF 增删该查 简单示例(一)

实现了那些功能,先看看效果图: 项目工程目录: 接下来开始具体的步骤: 第一步:在VS中新建工程 第二步:使用NuGet 安装EntityFramework 第三步:使用NuGet 安装EntityFramework.SqlSreverCompact 第四步:在Entities文件夹下添加StudentEntity类 1 public class StudentEntity 2 { 3 public int StudentId { get; set; } 4 public string Stud

WPF教程:MVVM模式的理解与应用

一.  什么是MVVM模式 MVVM是Model-View-ViewModel的简写.微软的WPF带来了新的技术体验,如Silverlight.音频.视频.3D.动画--.这导致了软件UI层更加细节化.可定制化.同时,在技术层面,WPF也带来了诸如Binding.Dependency Property.Routed Events.Command.DataTemplate.ControlTemplate等新特性.MVVM(Model-View-ViewModel)框架的由来便是MVP(Model-

wpf自定义Mvvm框架

1.DelegateCommand.cs 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows.Input; 7 8 namespace SimpleMvvmDemo.Commands 9 { 10 class DelegateCommand : ICom

CS文件类头注释

1.修改unity生成CS文件的模板(模板位置:Unity\Editor\Data\Resources\ScriptTemplates 文件名:81-C# Script-NewBehaviourScript.cs) 本人将模板修改为如下图(红框内的内容) 备注:在"#"之间的为可替换的参数 2.修改模板可替换参数,在工程项目Asset文件夹在创建Editor文件 在文件夹下添加AddFileHeadComment.cs文件 内容如下 参数内容根据个人需求修改

CS 和 BS 的区别和优缺点

bs是浏览器(browser)和服务器(server) cs是静态客户端程序(client)和服务器(server) 区别在于,虽然同样是通过一个程序连接到服务器进行网络通讯,但是bs结构的,客户端运行在浏览器里,比如你看百度,就是通过浏览器.还有一些bs结构的应用,比如中国电信,以及一些电子商务平台.用bs结构的好处是,不必专门开发一个客户端界面,可用asp,php,jsp等比较快速开发web应用的程序开发. cs结构的,要做一个客户端.网络游戏基本上大多是cs结构,比如你玩传奇,要专门开个传

微软SQLHelper.cs类 中文版

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Xml; using System.Collections; namespace LiuYanBanT { public class SqlHelper

AssemblyInfo.cs文件详解

一.前言 .net工程的Properties文件夹下自动生成一个名为AssemblyInfo.cs的文件,一般情况下我们很少直接改动该文件.但我们实际上通过另一个形式操作该文件.那就是通过在鼠标右键点击项目的属性进入“应用程序”->“程序集信息”,然后修改信息. 二.作用 通过特性(Attribute)来设置程序集(dll文件)的常规信息,供查看或作为配置信息供程序内部使用. 三.详解 // 程序集标题 [assembly:AssemblyTitle("程序集标题")] // 程

全局程序集GlobalAssemblyInfo.cs进行版本控制(引)

原文出自:http://blog.csdn.net/oyi319/article/details/5753311 1.全局程序集GlobalAssemblyInfo.cs 我们编写的一个解决方案,通常会包含多个项目,而每个项目都有单独的程序集信息AssemblyInfo.cs.但是,你会发现一个问题,这些AssemblyInfo.cs当中有一部分在重复的,若能把它们提取出来放入一个单一文件中,修改AssemblyInfo中的诸如产品名.产品版本.版本等信息会变得轻松.那么,这个程序集信息文件,我