代码之家  ›  专栏  ›  技术社区  ›  Vaccano

wpf simplecommand是否可以与泛型一起使用?

  •  2
  • Vaccano  · 技术社区  · 14 年前

    我正在使用此代码生成一个简单的命令:

    public class SimpleCommand : ICommand
    {
        public Predicate<object> CanExecuteDelegate { get; set; }
        public Action<object> ExecuteDelegate { get; set; }
    
        #region ICommand Members
    
        public bool CanExecute(object parameter)
        {
            if (CanExecuteDelegate != null)
                return CanExecuteDelegate(parameter);
            return true;// if there is no can execute default to true
        }
    
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    
        public void Execute(object parameter)
        {
            if (ExecuteDelegate != null)
                ExecuteDelegate(parameter);
        }
    
        #endregion
    }
    

    我没有写这个。但我喜欢用它。当我使用它时,结果是这样的:

    // This is the value that gets set to the command in the UI
    public SimpleCommand DoSomethingCommand { get; set; }
    
    
    public DoSomethingCommandConstructor()
    {
        DoSomethingCommand = new SimpleCommand
                            {
                                ExecuteDelegate = x => RunCommand(x)
                            };
    }
    
    private void RunCommand(object o)
    {
        // Run the command.
    }
    

    唯一的问题是runcommand的参数是一个对象。我想我被仿制药宠坏了。我总是希望IDE/编译器知道我使用的类型是OutCasting。

    是否可以使用泛型更改要实现的SimpleCommand类?

    1 回复  |  直到 13 年前
        1
  •  5
  •   Tim Cooper    13 年前

    当然。我们会向您指出Prism的实现,但CodePlex源选项卡似乎不起作用。它看起来像:

    public class SimpleCommand<T> : ICommand
    {
        public Predicate<T> CanExecuteDelegate { get; set; }
        public Action<T> ExecuteDelegate { get; set; }
    
        #region ICommand Members
    
        public bool CanExecute(object parameter)
        {
            if (CanExecuteDelegate != null)
                return CanExecuteDelegate((T)parameter);
            return true;// if there is no can execute default to true
        }
    
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    
        public void Execute(object parameter)
        {
            if (ExecuteDelegate != null)
                ExecuteDelegate((T)parameter);
        }
    
        #endregion
    }
    

    顺便说一下,您在问题中使用simplecommand有点迂回。而不是这个:

    DoSomethingCommand = new SimpleCommand
                        {
                            ExecuteDelegate = x => RunCommand(x)
                        };
    

    你只需要:

    DoSomethingCommand = new SimpleCommand
                        {
                            ExecuteDelegate = this.RunCommand
                        };
    

    指定lambda实际上只有在这样进行内联工作时才有用:

    DoSomethingCommand = new SimpleCommand
                        {
                            ExecuteDelegate = o => this.SelectedItem = o,
                            CanExecuteDelegate = o => o != null
                        };