WPF SimpleCommand 可以使用泛型吗?

发布于 2024-09-11 11:15:25 字数 1372 浏览 2 评论 0原文

我正在使用这段代码来创建一个简单命令:

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 类以使用泛型实现?

I am using this code to make a Simple Command:

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
}

I did not write this. But I enjoy using it. When I use it it ends up being like this:

// 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.
}

The only problem with this is that the parameter of RunCommand is an object. I think I have been spoiled by generics. I always want the IDE/compiler to just know what the type I am working with is with out casting.

Is it possible to change this SimpleCommand class to be implemented using generics?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很快妥协 2024-09-18 11:15:25

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

Sure. Was gonna point you to Prism's implementation, but CodePlex source tab seems to not be working. It would look something like:

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
}

Incidentally, your usage of SimpleCommand in your question is a little roundabout. Instead of this:

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

You could just have:

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

Specifying a lambda is really only useful if you're doing the work inline like this:

DoSomethingCommand = new SimpleCommand
                    {
                        ExecuteDelegate = o => this.SelectedItem = o,
                        CanExecuteDelegate = o => o != null
                    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文