如何防止关闭窗口上的应用程序关闭?

时间:2021-12-01 17:04:51

I'm using WPF and I've got no main window (I've overwritten the OnStartup method). But when user clicks on some menu-item, I want to show the settings window.

我正在使用WPF而且我没有主窗口(我已经覆盖了OnStartup方法)。但是当用户点击某个菜单项时,我想显示设置窗口。

App.xaml.cs:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    new MainEnvironment();
}

MainEnvironment.cs:

NotifyIcon notifyIcon;
Settings settings_wnd = new Settings(); // WPF window

public MainEnvironment()
{
    notifyIcon = new NotifyIcon()
    {
        ...

        ContextMenu = new ContextMenu(new MenuItem[]
        {
            new MenuItem("Settings", contextMenu_settingsButton_Click)
        })
    };
}

void contextMenu_settingsButton_Click(object sender, EventArgs e)
{
    if (!this.settings_wnd.IsVisible)
        this.settings_wnd.Show();
    else
        this.settings_wnd.Activate();
}

Problem is that when user closes this window, the whole application exits too. Why? And how can I prevent that?

问题是当用户关闭此窗口时,整个应用程序也会退出。为什么?我怎么能防止这种情况?

Thanks

1 个解决方案

#1


6  

The application is defaultly set to shutdown when all its windows are closed. You only need to add ShutdownMode="OnExplicitShutdown" to your App.xaml file.

应用程序默认设置为关闭所有窗口时关闭。您只需要将ShutdownMode =“OnExplicitShutdown”添加到App.xaml文件中。

#1


6  

The application is defaultly set to shutdown when all its windows are closed. You only need to add ShutdownMode="OnExplicitShutdown" to your App.xaml file.

应用程序默认设置为关闭所有窗口时关闭。您只需要将ShutdownMode =“OnExplicitShutdown”添加到App.xaml文件中。