我正在研究Silverlight 和 WPF 中企业架构的 MVVM 生存指南,并在命令部分遇到了障碍。具体来说,它基于 Action<object> 和 Func<object, bool> 创建一个命令。在我应该“构建和运行应用程序”的时候,我却得到了编译错误。
命令内容:
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
正确实现。
为了节省我的前额/最近的墙,有人可以告诉我我做错了什么以便我可以修复它,或者给定的(书)代码让我出错,所以我可以继续前进。