I have a closecommand defined inside my viewmodel for my dialog window. I have another command defined inside that viewmodel. Now I have that command binded to a control in my view. After performing certain command actions, I want it to call closecommand to close the window. Is that possible?
问问题
1265 次
1 回答
3
是的。您可以使用一个 CompositeCommand 包装两个(或任意数量)您的其他命令。我相信这是在 Prism 中,但是如果您在项目中无法访问它,那么您自己实现类似的功能并不是非常困难,特别是如果您不使用参数 - 您所做的就是实现 ICommand有一个类,然后在类中有一个私有的 ICommand 列表。
以下是关于 Prism 的 CompositeCommand 类的更多信息:
下面是我自己公认的简短且可能不规范的实现。要使用它,您需要做的就是在您的 VM 上引用它,然后绑定到它。您可以为要运行的所有其他命令调用 .AddCommand。棱镜的实施方式可能有所不同,但我相信这会奏效:
public class CompositeCommand : ICommand {
private List<ICommand> subCommands;
public CompositeCommand()
{
subCommands = new List<ICommand>();
}
public bool CanExecute(object parameter)
{
foreach (ICommand command in subCommands)
{
if (!command.CanExecute(parameter))
{
return false;
}
}
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
foreach (ICommand command in subCommands)
{
command.Execute(parameter);
}
}
public void AddCommand(ICommand command)
{
if (command == null)
throw new ArgumentNullException("Yadayada, command is null. Don't pass null commands.");
subCommands.Add(command);
}
}
于 2011-07-06T18:39:36.757 回答