我有一个DelegateCommand的子类,它失败并出现InvalidProgramException,“Command Language Runtime检测到一个无效的程序。”当我打电话给执行时。
我的子类看起来像这样:
public class SaveOrderCommand : DelegateCommand<IOrder>
{
[Dependency]
public IOrderService OrderService { get; set; }
public SaveOrderCommand()
: base(p => this.ExecuteSaveOrder(p), p => this.CanExecuteSaveOrder(p))
{ }
public bool CanExecuteSaveOrder(IOrder order)
{
return true;
}
public void ExecuteSaveOrder(IOrder order)
{
if (order == null)
return;
IProductOrderService productSpecificOrderService = this.OrderService.RegisteredServices[order.GetType()] as IProductOrderService;
order = productSpecificOrderService.Save(order);
}
}
我可以看到给基类引用子类中的方法可能是一个问题,但如果这是问题,那么有人可以建议更好的实现吗?
答案 0 :(得分:1)
也许您应该自己实施ICommand
?你的班级将成为:
public class SaveOrderCommand : ICommand
{
[Dependency]
public IOrderService OrderService { get; set; }
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return parameter is IOrder;
}
public void Execute(object parameter)
{
var order = parameter as IOrder;
if (order == null)
return;
IProductOrderService productSpecificOrderService = this.OrderService.RegisteredServices[order.GetType()] as IProductOrderService;
order = productSpecificOrderService.Save(order);
}
}