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

发布于 2024-11-18 13:23:16 字数 113 浏览 4 评论 0原文

我在对话框窗口的视图模型中定义了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪恋 2024-11-25 13:23:16

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

以下是有关 Prism 中 CompositeCommand 类的更多信息:

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

我自己的公认的简短且可能不规范的实现如下。要使用它,您所需要做的就是在您的虚拟机上引用它,然后绑定到它。您可以为要运行的所有其他命令调用 .AddCommand。也许 Prism 的实现方式有所不同,但我相信这会起作用:

    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);
    }
}

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:

    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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文