【发布时间】:2013-04-24 08:14:33
【问题描述】:
我正在处理MVVM Survival Guide for Enterprise Architectures in Silverlight and WPF 并在命令部分遇到了障碍。具体来说,它基于 Action
命令内容:
public class Command : ICommand
{
private readonly Action<object> _execute;
private readonly Func<object, bool> _canExecute;
public Command(Action<object> execute, Func<object, bool> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public void Execute(object parameter)
{
_execute(parameter);
}
public bool CanExecute(object parameter)
{
return (_canExecute == null) || _canExecute(parameter);
}
...
}
方法调用的东西:
private Command _showDetailsCommand;
public Command ShowDetailsCommand
{
get
{
return _showDetailsCommand
?? (_showDetailsCommand
= new Command(ShowCustomerDetails, IsCustomerSelected));
}
}
public void ShowCustomerDetails()
{
if (!IsCustomerSelected()){
throw new InvalidOperationException("Unable to display customer details. "
+ "No customer selected.");
}
CustomerDetailsTabViewModel customerDetailsViewModel =
GetCustomerDetailsTab(SelectedCustomerID);
if (customerDetailsViewModel == null)
{
customerDetailsViewModel
= new CustomerDetailsTabViewModel
(_dataProvider,
SelectedCustomerID);
Tabs.Add(customerDetailsViewModel);
}
SetCurrentTab(customerDetailsViewModel);
}
private bool IsCustomerSelected()
{
return !string.IsNullOrEmpty(SelectedCustomerID);
}
new Command(ShowCustomerDetails, IsCustomerSelected)) 位下方出现蓝色波浪线,并带有“Northwind.Application.Command.Command(System.Action<object>, System.Func<object, bool>) 的最佳重载匹配有一些无效参数”。
当我尝试编译时,我收到上述错误,以及两条消息:
Argument 1: Cannot convert from method group to System.Action<object>
Argument 2: Cannot convert from method group to System.Func<object, bool>
现在,我对 Actions 和 Funcs 的了解比昨天更多,并且几乎可以通过将命令声明更改为:
private readonly Action _execute;
private readonly Func<bool> _canExecute;
并在整个代码中执行类似操作,但随后我收到一条错误消息,提示我没有正确实现 ICommand。
为了节省我的前额/最近的墙,有人可以告诉我我做错了什么以便我可以修复它,或者给定的(书)代码让我出错,所以我可以继续前进。
【问题讨论】:
-
动作和函数都需要一个对象参数,但是你传入的方法没有任何参数。
标签: c# wpf mvvm icommand relaycommand