泛型可以使用 WPF SimpleCommand 吗?

WPF SimpleCommand possible with generics?(泛型可以使用 WPF SimpleCommand 吗?)
本文介绍了泛型可以使用 WPF 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.
}

唯一的问题是 RunCommand 的参数是一个对象.我想我已经被仿制药宠坏了.我总是希望 IDE/编译器只知道我正在使用的类型是什么而不进行强制转换.

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.

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

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

推荐答案

当然.本打算将您指向 Prism 的实现,但 CodePlex 源选项卡似乎无法正常工作.它看起来像:

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
}

顺便说一句,您在问题中对 SimpleCommand 的使用有点迂回.而不是这个:

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

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

你可以有:

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

仅当您像这样进行内联工作时,指定 lambda 才真正有用:

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
                    };

这篇关于泛型可以使用 WPF SimpleCommand 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)