c# - 使用 CanExecute 参数调用 RelayCommand<T>

标签 c# wpf mvvm

如何通过传入 CanExecute 参数来调用 RelayCommand。该对象被绑定(bind)为 View 中的命令参数。我有以下代码,上面写着“Delegate Func does not take 0 arguments”。请帮忙。 OnPrintAllChartsInCurrentView 和 IsPrintAndExportEnabled 的定义如下。

     RelayCommand m_PrintAllChartsCommand;
                public ICommand PrintAllChartsCommand
                {
                    get
                    {
                        return m_PrintAllChartsCommand ?? (m_PrintAllChartsCommand = new RelayCommand<object>(
                            OnPrintAllChartsInCurrentView,
                            () => IsPrintAndExportEnabled() //This line there is a compiler error
                            ));
                    }
                }

private void OnPrintAllChartsInCurrentView(object obj)
        {
//do something. The obj is bound as command parameter from the view.
        }

    private bool IsPrintAndExportEnabled()
    {
    //I will do some operation here and change to return true or false later
    return false;
    }

这是我要调用的 RelayCommand 类

namespace GalaSoft.MvvmLight.Command
{

    public class RelayCommand<T> : ICommand
    {
        //
        // Summary:
        //     Initializes a new instance of the RelayCommand class that can always execute.
        //
        // Parameters:
        //   execute:
        //     The execution logic.
        //
        // Exceptions:
        //   T:System.ArgumentNullException:
        //     If the execute argument is null.
        public RelayCommand(Action<T> execute);
        //
        // Summary:
        //     Initializes a new instance of the RelayCommand class.
        //
        // Parameters:
        //   execute:
        //     The execution logic.
        //
        //   canExecute:
        //     The execution status logic.
        //
        // Exceptions:
        //   T:System.ArgumentNullException:
        //     If the execute argument is null.
        public RelayCommand(Action<T> execute, Func<T, bool> canExecute);

        //
        // Summary:
        //     Occurs when changes occur that affect whether the command should execute.
        public event EventHandler CanExecuteChanged;

        //
        // Summary:
        //     Defines the method that determines whether the command can execute in its current
        //     state.
        //
        // Parameters:
        //   parameter:
        //     Data used by the command. If the command does not require data to be passed,
        //     this object can be set to a null reference
        //
        // Returns:
        //     true if this command can be executed; otherwise, false.
        public bool CanExecute(object parameter);
        //
        // Summary:
        //     Defines the method to be called when the command is invoked.
        //
        // Parameters:
        //   parameter:
        //     Data used by the command. If the command does not require data to be passed,
        //     this object can be set to a null reference
        public virtual void Execute(object parameter);
        //
        // Summary:
        //     Raises the GalaSoft.MvvmLight.Command.RelayCommand`1.CanExecuteChanged event.
        public void RaiseCanExecuteChanged();
    }
}

最佳答案

如果您使用 T 类型的命令参数(您将其声明为对象),则 RelayCommand CanExecute 需要一个 T 类型的参数并返回 bool。您正在传递一个匿名函数,它不接受任何参数并返回一个 bool 值。你可以简单地替换

() => IsPrintAndExportEnabled();

arg => { return IsPrintAndExportEnabled(); }

如果您不打算对传递给 CanExecute 的对象执行任何操作。

如果不需要命令参数,则不需要将 RelayCommand 声明为

RealyCommand<object>(execute, canExecute);

可以简单的

RelayCommand(execute, canExecute);

在这种情况下,Execute 将不带任何参数并返回 void,而 CanExecute 将不带任何参数并返回 bool。

关于c# - 使用 CanExecute 参数调用 RelayCommand<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49546619/

相关文章:

c# - VB.net 中没有增量运算符

c# - NHibernate 中的自然排序

wpf - WPF 不支持 10 位显示器(又名 30 位颜色)吗?

c# - WPF 中作用域 EF Core DbContext 的生命周期

javascript - applyBindingsToNode 与 observable

javascript - knockout : computed observables in cascaded arrays (complex viewmodel) and the this-pointer

c# - Microsoft.Reporting.WebForms.ReportViewer contol 在 ToolBar 中添加自定义控件

c# - 使用正则表达式提取匹配文本

wpf - WPF 中的上下文菜单继承

c# - WPF MVVM : Postpone rendering of View until DataContext is set