如何通过传入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();
}
}
答案 0 :(得分:1)
如果要获取类型为T的命令参数(您声明为对象),则RelayCommand CanExecute需要一个类型为T的参数并返回bool。你传入一个匿名函数,它不带参数并返回一个bool。你可以简单地替换
() => IsPrintAndExportEnabled();
与
arg => { return IsPrintAndExportEnabled(); }
如果您不打算对传递给CanExecute的对象执行任何操作。
如果您不需要命令参数,则无需将RelayCommand声明为
RealyCommand<object>(execute, canExecute);
它可以简单地
RelayCommand(execute, canExecute);
在这种情况下,Execute将不接受任何参数并返回void,而CanExecute将不接受任何参数并返回bool。
答案 1 :(得分:1)
应该是这样的。请注意,RelayCommand canExecute参数为Func<T,bool>
,这意味着您传递具有相同签名的方法(如下所示)。有关Func<T,TResult>
的更多信息,请参阅this。
RelayCommand m_PrintAllChartsCommand;
public ICommand PrintAllChartsCommand
{
get
{
return m_PrintAllChartsCommand ?? (m_PrintAllChartsCommand = new RelayCommand<object>(
OnPrintAllChartsInCurrentView,
IsPrintAndExportEnabled
));
}
}
private void OnPrintAllChartsInCurrentView(object arg)
{
}
private bool IsPrintAndExportEnabled(object arg)
{
return false;
}
答案 2 :(得分:1)
这对我有用(IsRealShunter是一个布尔属性):
RaiseAlarmClickCommand = new RelayCommand<Guid>(RaiseAlarmClick, x => IsRealShunter);
RaiseAlarmClick是一种方法:
private void RaiseAlarmClick(Guid idPrinter) {...}
一个小注意事项,如果您的视图模型中有更新内容,并且您想确保您的RelayCommand反映更改,请从发生更改的地方调用此内容:
RaiseAlarmClickCommand.RaiseCanExecuteChanged();
答案 3 :(得分:0)
这对我有用。如果它可以帮助任何人。首先看看RelayCommand,我声明的方式是错误的。它应该是RelayCommand<object>
这是完整的声明和用法。
RelayCommand<object> m_PrintAllChartsCommand;
public ICommand PrintAllChartsCommand
{
get
{
return m_PrintAllChartsCommand ?? (m_PrintAllChartsCommand = new RelayCommand<object>(
OnPrintAllChartsInCurrentView, IsPrintAndExportEnabled));
}
}
private void OnPrintAllChartsInCurrentView(object arg)
{
}
private bool IsPrintAndExportEnabled(object arg)
{
}