【发布时间】:2023-04-03 18:50:01
【问题描述】:
如何通过传入 CanExecute 参数来调用 RelayCommand。该对象被绑定为视图中的命令参数。我有以下一段代码,上面写着“Delegate Func 不接受 0 个参数”。请帮忙。 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();
}
}
【问题讨论】:
-
Execute 和 CanExecute 必须具有相同的签名和参数....缺少对象:param => { return IsPrintAndExportEnabled(param); }