我可以在命令中调用命令吗?
我在对话框窗口的视图模型中定义了一个 close 命令。我在该视图模型中定义了另一个命令。现在,我已将该命令绑定到我视图中的控件。执行某些命令操作后,我希望它调用 closecommand 来关闭窗口。这可能吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。您可以使用 CompositeCommand 来包装两个(或任意数量)其他命令。我相信这是在 Prism 中,但如果您无法在项目中访问它,那么自己实现类似的功能并不是非常困难,特别是如果您不使用参数 - 您所做的就是实现 ICommand与一个类,然后在该类中有一个私有的 ICommand 列表。
以下是有关 Prism 中 CompositeCommand 类的更多信息:
http://msdn.microsoft.com/en-us/library/microsoft.practices.composite.presentation.commands.compositecommand_members.aspx
我自己的公认的简短且可能不规范的实现如下。要使用它,您所需要做的就是在您的虚拟机上引用它,然后绑定到它。您可以为要运行的所有其他命令调用 .AddCommand。也许 Prism 的实现方式有所不同,但我相信这会起作用:
Yes. You can use a CompositeCommand that wraps both (or any number) of your other commands. I believe this is in Prism, but if you don't have access to that in your project, it isn't terribly difficult to implement similar functionality on your own, especially if you're not using parameters - all you do is implement ICommand with a class and then have a private List of ICommands inside the class.
Here's more on the CompositeCommand class from Prism:
http://msdn.microsoft.com/en-us/library/microsoft.practices.composite.presentation.commands.compositecommand_members.aspx
My own admittedly short and possibly non-canonical implementation follows. To use it, all you need to do is have this be referenced on your VM, and then bind to it instead. You can call .AddCommand for all the other commands that you want to run. Probably the Prism one is implemented differently, but I believe this will work: