我可以在命令中调用命令吗?

我在我的viewmodel中为我的对话窗口定义了一个closecommand。 我在viewmodel中定义了另一个命令。 现在我将该命令绑定到我视图中的控件。 执行某些命令操作后,我希望它调用close命令关闭窗口。 那可能吗?

是。 您可以使用包含两个(或任意数量)其他命令的CompositeCommand。 我相信这是在Prism中,但是如果你在项目中没有访问它,那么自己实现类似的function并不是非常困难,特别是如果你没有使用参数 – 你所做的就是实现ICommand使用类,然后在类中有一个私有的ICommands列表。

以下是Prism的CompositeCommand类的更多内容:

http://msdn.microsoft.com/en-us/library/microsoft.practices.composite.presentation.commands.compositecommand_members.aspx

我自己肯定是短暂的,可能是非规范的实现。 要使用它,您需要做的就是在VM上引用它,然后绑定到它。 您可以为要运行的所有其他命令调用.AddCommand。 可能Prism的实现方式不同,但我相信这会有效:

public class CompositeCommand : ICommand { private List subCommands; public CompositeCommand() { subCommands = new List(); } 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); } }