非泛型类型“DelegateCommand”不能与类型参数一起使用

时间:2016-12-10 01:27:50

标签: c# wpf uwp

 public ICommand OpenDialogFile
        {
            get
            {
                return new DelegateCommand<RichEditBox>(OpenDialogToAttach);
            }
        }

Command="{Binding OpenDialogFile}" CommandParameter="{Binding ElementName=TweetEditBox}"

所以,DeledateCommand doest了解RichEditBox。虽然我使用“

  

OpenDialogToAttach(RichEditBox编辑框)”。

如何解决?我开发UWP。

这是一个DelegateCommand Code.Initializes DelegateCommand类的一个新实例

 internal class DelegateCommand : ICommand
{
    private readonly Action _execute;

    private readonly Func<bool> _canExecute;

    /// <summary>
    /// Initializes a new instance of the RelayCommand class that 
    /// can always execute.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    /// <exception cref="ArgumentNullException">If the execute argument is null.</exception>
    public DelegateCommand(Action execute)
        : this(execute, null)
    {
    }

    /// <summary>
    /// Initializes a new instance of the RelayCommand class.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    /// <param name="canExecute">The execution status logic.</param>
    /// <exception cref="ArgumentNullException">If the execute argument is null.</exception>
    public DelegateCommand(Action execute, Func<bool> canExecute)
    {
        if (execute == null)
        {
            throw new ArgumentNullException("execute");
        }

        _execute = execute;
        _canExecute = canExecute;
    }

    /// <summary>
    /// Occurs when changes occur that affect whether the command should execute.
    /// </summary>
    public event EventHandler CanExecuteChanged;

    /// <summary>
    /// Raises the <see cref="CanExecuteChanged" /> event.
    /// </summary>
    [SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate",
        Justification = "This cannot be an event")]
    public void RaiseCanExecuteChanged()
    {
        var handler = CanExecuteChanged;
        if (handler != null)
        {
            handler(this, EventArgs.Empty);
        }
    }

    /// <summary>
    /// Defines the method that determines whether the command can execute in its current state.
    /// </summary>
    /// <param name="parameter">This parameter will always be ignored.</param>
    /// <returns>true if this command can be executed; otherwise, false.</returns>
    [DebuggerStepThrough]
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute();
    }

    /// <summary>
    /// Defines the method to be called when the command is invoked. 
    /// </summary>
    /// <param name="parameter">This parameter will always be ignored.</param>
    public void Execute(object parameter)
    {
        if (CanExecute(parameter))
        {
            _execute();
        }
    }
}

3 个答案:

答案 0 :(得分:0)

您正在使用的DelegateCommand类的实现不支持您尝试传递给它的泛型参数。

return new DelegateCommand<RichEditBox>(OpenDialogToAttach)

您可以使用Microsoft.Practices.Composite.Presentation.CommandsDelegateCommand类的实现。这是一个带有泛型参数的实现。

答案 1 :(得分:0)

您的班级DelegateCommand不是通用的,因此您无法像尝试那样使用类型参数RichEditBox对其进行实例化。

你有一个接受动作的构造函数,所以你可以方便地将lambda传递给它,捕获你需要的上下文,例如:

return new DelegateCommand(x => {
    RichEditBox richBox = (RichEditBox) x;
    OpenDialogToAttach()...
});

答案 2 :(得分:0)

您将在GitHub上找到UWP的通用DelegateCommand类的Prism实现:https://github.com/PrismLibrary/Prism/blob/master/Source/Prism/Commands/DelegateCommand%7BT%7D.cs

您可以用自己的实现替换自己的实现,即将类定义复制到项目中。您还需要基类:https://github.com/PrismLibrary/Prism/blob/master/Source/Prism/Commands/DelegateCommandBase.cs

或者,您可以使用NuGet(工具 - &gt; Nuget包管理器 - &gt; Visual Studio中的包管理器控制台)安装Prism.Windows包:https://www.nuget.org/packages/Prism.Windows/

...并直接在代码中使用DelegateCommand:

public ICommand OpenDialogFile
    {
        get
        {
            return new Prism.Commands.DelegateCommand<RichEditBox>(OpenDialogToAttach);
        }
    }