zoukankan      html  css  js  c++  java
  • ICommand接口

    ICommand 接口是常用接口,在wpf中,经常会为一个button或者一个link绑定一个command

       public interface ICommand
        {
            // 摘要:
            //     当出现影响是否应执行该命令的更改时发生。
            event EventHandler CanExecuteChanged;

            // 摘要:
            //     定义用于确定此命令是否可以在其当前状态下执行的方法。
            //
            // 参数:
            //   parameter:
            //     此命令使用的数据。如果此命令不需要传递数据,则该对象可以设置为 null。
            //
            // 返回结果:
            //     如果可以执行此命令,则为 true;否则为 false。
            bool CanExecute(object parameter);
            //
            // 摘要:
            //     定义在调用此命令时调用的方法。
            //
            // 参数:
            //   parameter:
            //     此命令使用的数据。如果此命令不需要传递数据,则该对象可以设置为 null。
            void Execute(object parameter);
        }

    这是VS中icommand的原型,一个事件,一个Action(无返回值的委托函数),一个Func(有返回值的委托函数)

    新建一个FishCommand类,继承ICommand接口,实现它的两个方法,在这里,类的构造函数里直接传入一个Action和一个Func

     class FishCommand : ICommand
        {
            private Func<object, bool> _CanExecute;
            private Action<object> _Execute;

            public FishCommand(FishExecute execute,FishCanExecute canExecuteFunc)
            {
                _CanExecute = canExecuteFunc;
                _Execute = execute;
            }

            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }

            public bool CanExecute(object parameter)
            {
                return _CanExecute == null ? true : _CanExecute(parameter);
            }

            public void Execute(object parameter)
            {
                if (_Execute != null)
                {
                    _Execute(parameter);
                }
            }
        }

    这里做一个假设,假设一个button,它的功能是要做一个10秒钟的任务,当然啦,在任务进行时,这个按钮状态是不能用的,知道任务结束,按钮才能继续用,简单的一个方案是 在button的 enable属性上绑定一个bool属性,让command执行Action 的时候把属性置为false,当func结束时,再把属性置为true,就能很好的进行控制,当然你也可以在button中写一个bool的依赖属性,像这样

     public  class FishButton:Button
        {
          public static readonly DependencyProperty IsBusyProperty = DependencyProperty.Register("IsBusy", typeof(Boolean), typeof(FishButton));

          public bool IsBusy
          {
              get { return (bool)GetValue(IsBusyProperty); }
              set { SetValue(IsBusyProperty, value); }
          }
        }

      <fish:FishButton Width="60" Height="40" Content="click" Command="{Binding IsClick}" Grid.Column="0" Grid.Row="1" IsBusy="{Binding IsFree}"/>

    直接绑定依赖属性一方面更简洁,另一方面,也更加效率


            private ICommand _isClick;
            public ICommand IsClick
            {
                get
                {
                    if (_isClick == null)
                    {
                        _isClick = new FishCommand(data =>
                        {

              IsFree = false;
                           //这里写你的方法,可以用异步的方式,就不会让button卡在那里

              IsFree = false;
                        }, o => IsFree);
                    }
                    return _isClick;
                }
            }

    这里我是在Action中直接设置属性,wpf的通知机制会帮你处理好binding,这样的话,就能够很好的用一个属性在command中控制button的状态

            bool CanExecute(object parameter); 这个方法就是可以控制button命令是否能用的方法

    若有兴趣,可以联系,讨论

    qq 124312457

  • 相关阅读:
    eclipse调试(debug)的时候,出现Source not found,Edit Source Lookup Path,一闪而过
    MyEclipse中的查找快捷键
    eclipse和myeclipse怎么在项目中查找指定代码?https://www.jb51.net/softjc/554889.html
    Spring如何加载XSD文件(org.xml.sax.SAXParseException: Failed to read schema document错误的解决方法)
    Spring的应用上下文ApplicationContext
    springmvc中获取request对象,加载biz(service)的方法
    Spring中RequestContextHolder以及HandlerInterceptorAdapter的使用
    Spring 单元测试 RequestContextHolder.getRequestAttributes()).getRequest(); 为空的原因
    Lua整理——table库
    POJ 2377 Bad Cowtractors
  • 原文地址:https://www.cnblogs.com/fish124423/p/2505924.html
Copyright © 2011-2022 走看看