0
votes

Currently i have a strange behavior in my WPF Application. I have a Main Window and a Settings Dialog. I can show the Settings Dialog, but when i close it, also my Main Window will be closed and the Application quits.

The ShutdownMode of the Application is OnMainWindowClose. But this also happens with OnLastWindowClose.

This is the Code of my Controller class that controls the MainWindow and the Settings Window:

public class MainWindowController : IDisposable, IMainWindowController
{
    private readonly MainWindowViewModel mainWindowViewModel;
    private readonly MainWindow mainWindow;

    private IoAdapter ioAdapter;

    public MainWindowController()
    {
        this.mainWindowViewModel = new MainWindowViewModel(this);
        this.mainWindow = new MainWindow { DataContext = this.mainWindowViewModel };
    }

    public void ShowMainWindow()
    {
        mainWindow.Show();
    }

    public void ShowSettingsWindow()
    {
        var viewModel = new SettingsWindowViewModel();

        var settingsWindow = new SettingsWindow { DataContext = viewModel, Owner = this.mainWindow};

        var showDialog = settingsWindow.ShowDialog();
        if (showDialog != null && showDialog.Value)
        {
            // Apply Settings
        }
    }
1
how does it behave if you remove completely the method ShowMainWindow ? I think it's not needed as main window will be shown anyway on start. how is your code in Apply Settings? - Davide Piras
I need to show the mainWindow with Show() because i removed the StartUpUri in the App.xaml. Currently there isn't any code for applying the Settings. There is only the comment - Noffls
yes and if you do as I said and readd it to app.xaml can you reproduce or is it working normally then? - Davide Piras
Adding the MainWindow as the StartupUri brought me to the right solution. The Problem was that i created the MainWindowController in the constructor of App class. So the MainWindow was also created at this moment. Moving the creation of the MainWindowController into the OnStartup solved the problem. So thank you. - Noffls
Bravo and thanks for feedback :) - Davide Piras

1 Answers

0
votes

you should remove completely your method:

 public void ShowMainWindow()
    {
        mainWindow.Show();
    }

and have the MainWindow displayed as normally in the StartupUri of your App.xaml