【问题标题】:WPF SimpleCommand possible with generics?泛型可以使用 WPF SimpleCommand 吗?
【发布时间】:2010-07-27 17:50:03
【问题描述】:

我正在使用这段代码来制作一个简单的命令:

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/编译器只知道我正在使用的类型是什么而不进行强制转换。

是否可以将这个 SimpleCommand 类更改为使用泛型实现?

【问题讨论】:

    标签: c# wpf generics command


    【解决方案1】:

    当然。本来打算将您指向 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
                        };
    

    【讨论】:

    猜你喜欢
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 2021-05-17
    • 2011-03-22
    相关资源
    最近更新 更多