zoukankan      html  css  js  c++  java
  • 菲佣的WPF——1

    所有外包程序员都是菲佣

    这个开头自娱自乐。今天就发我自己写的WPF DeletegateCommand 类。用于ICommnad的实现

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Input;
    
    namespace WpfApplication4
    {
        public class DeletegateCommand : ICommand
        {
            #region << Field >>
            private readonly Action<object> excute;
            private readonly Func<object, bool> canExcute;
            #endregion
    
            #region << Constructor >>
            public DeletegateCommand(Action excute)
                : this((o)=>excute(),(o)=>true)
            { }
    
            public DeletegateCommand(Action excute, Func<bool> canExcute)
                : this((o) => excute(), (o) => canExcute())
            { }
    
            public DeletegateCommand(Action<object> excute)
                : this(excute, (o) => true)
            { }
    
            public DeletegateCommand(Action<object> excute, Func<bool> canExcute)
                : this(excute, (o) => canExcute())
            { }
    
            public DeletegateCommand(Action<object> excute, Func<object, bool> canExcute)
            {
                this.excute = excute;
                this.canExcute = canExcute;
            }
            #endregion
    
            #region << Method >>
            public bool CanExecute(object parameter)
            {
                return canExcute(parameter);
            }
    
            public event EventHandler CanExecuteChanged
            {
                add
                {
                    CommandManager.RequerySuggested += value;
                }
                remove
                {
                    CommandManager.RequerySuggested -= value;
                }
            }
    
            public void Execute(object parameter)
            {
                excute(parameter);
            }
            #endregion
        }
    
        public class DeletegateCommand<T> : DeletegateCommand
        {
            #region << Constructor >>
            public DeletegateCommand(Action<T> excute)
                : this(excute, (o) => true)
            { }
    
            public DeletegateCommand(Action<T> excute, Func<bool> canExcute)
                : this(excute, (o) => canExcute())
            { }
            public DeletegateCommand(Action<T> excute,Func<T,bool> canExcute):base((o)=>excute((T)o),(o)=>canExcute((T)o))
            {
            }
            #endregion
    
            #region << Method >>
            public bool CanExecute(T parameter)
            {
                return base.CanExecute(parameter);
            }
    
            public void Execute(T parameter)
            {
                base.Execute(parameter);
            }
            #endregion
        }
    }

    上面是DelegateCommand的实现。下面是使用的代码。超级简约(只显示ViewModel,XAML的command的就不显示了)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    
    namespace WpfApplication4
    {
        public class MainWindowsViewModel
        {
            #region << Property >>
            public ICommand DemoCommand { get; set; }
            #endregion
    
            #region << Constructor >>
            public MainWindowsViewModel()
            {
                DemoCommand = new DeletegateCommand<string>(DemoMethod);
            }
            #endregion
    
            #region << Method >>
            public void DemoMethod(string input)
            {
                MessageBox.Show(input as string);
            }
    
            public bool CanDemoMethod()
            {
                return false;
            }
            #endregion
    
        }
    }
  • 相关阅读:
    Python执行Linux系统命令的4种方法
    linux之sed用法
    Struts中使用json-lib-2.4-jdk15.jar时抛出Source not found
    警告: couldn't clear tomcat cache
    Mysql数据库中图片字段Blob类型和String类型相互转换
    严重: Exception starting filter struts2 解决办法
    解决Tomcat启动时出现的The APR based Apache Tomcat Native library异常
    严重: Error, processing connection
    Android开发时,sqlite创建表成功,insert不报错,但没有数据插入的原因
    getOutputStream() has already been called for this response异常的原因和解决方法
  • 原文地址:https://www.cnblogs.com/qiurideyun/p/2910388.html
Copyright © 2011-2022 走看看