Kolan
Kolan

Reputation: 103

How to prevent application shutdown on close window?

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.

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

Upvotes: 5

Views: 3682

Answers (1)

Martin Heralecký
Martin Heralecký

Reputation: 5779

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.

Upvotes: 13

Related Questions