Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent application shutdown on close window?

Tags:

c#

wpf

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

like image 436
Kolan Avatar asked Dec 19 '22 01:12

Kolan


1 Answers

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.

like image 152
Martin Heralecký Avatar answered Dec 28 '22 22:12

Martin Heralecký