如何从上下文菜单关闭 WPF 应用程序?

发布于 2024-09-26 12:38:37 字数 102 浏览 0 评论 0原文

WPF 中是否有用于从上下文菜单关闭应用程序的命令?也就是说,右键单击任何窗口的标题栏所获得的上下文菜单相同吗?

有很多标准命令,但我正在努力寻找退出命令。

Is there a command in WPF to close the application from context menu? That is, the same context menu which you get by right clicking on the title bar on any window?

There are loads of standard commands, but I am struggling to find an exit command.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

故事未完 2024-10-03 12:38:37

不幸的是,这并不存在。您必须实现自定义命令并调用

Application.Current.Shutdown();

Unfortunately this doesn't exist. You'll have to implement a custom command and call

Application.Current.Shutdown();
夜巴黎 2024-10-03 12:38:37

有一个ApplicationCommands.Close,但没有一个ApplicationCommands.Exit。

请参阅此线程(例如)以查找替代方案(例如创建自定义命令) 。

There is an ApplicationCommands.Close, but there is not an ApplicationCommands.Exit.

See this thread (for example) to find alternatives (such as creating a custom command).

不乱于心 2024-10-03 12:38:37

你的问题已经解决了。但以下代码可能对其他人有帮助。

     Environment.Exit(0)

Your problem is solved. But following code may help others.

     Environment.Exit(0)
聽兲甴掵 2024-10-03 12:38:37

实际上并没有那么复杂(但是,M$ 不提供它仍然很糟糕)。给你:

public static class MyCommands
{
    private static readonly ICommand appCloseCmd = new ApplicationCloseCommand();
    public static ICommand ApplicationCloseCommand
    {
        get { return appCloseCmd; }
    }
}

//===================================================================================================
public class ApplicationCloseCommand : ICommand
{
    public event EventHandler CanExecuteChanged
    {
        // You may not need a body here at all...
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public bool CanExecute(object parameter)
    {
        return Application.Current != null && Application.Current.MainWindow != null;
    }

    public void Execute(object parameter)
    {
        Application.Current.MainWindow.Close();
    }
}

并且甚至可能不需要 AplicationCloseCommand.CanExecuteChanged 事件处理程序的主体。

你可以像这样使用它:

<MenuItem Header="{DynamicResource MenuFileExit}" Command="MyNamespace:MyCommands.ApplicationCloseCommand"/>

干杯!

(你无法想象我自己花了多长时间才发现这个命令的东西......)

Not that complex actually (but still, M$ sucks for not providing it). Here you go:

public static class MyCommands
{
    private static readonly ICommand appCloseCmd = new ApplicationCloseCommand();
    public static ICommand ApplicationCloseCommand
    {
        get { return appCloseCmd; }
    }
}

//===================================================================================================
public class ApplicationCloseCommand : ICommand
{
    public event EventHandler CanExecuteChanged
    {
        // You may not need a body here at all...
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public bool CanExecute(object parameter)
    {
        return Application.Current != null && Application.Current.MainWindow != null;
    }

    public void Execute(object parameter)
    {
        Application.Current.MainWindow.Close();
    }
}

And the body of the AplicationCloseCommand.CanExecuteChanged event handler may not be even needed.

You use it like so:

<MenuItem Header="{DynamicResource MenuFileExit}" Command="MyNamespace:MyCommands.ApplicationCloseCommand"/>

Cheers!

(You cannot imagine how long it took me to discover this Command stuff myself...)

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