WPF showing dialog before main window

Viewed 41397

How one can show dialog window (e.g. login / options etc.) before the main window?

Here is what I tried (it apparently has once worked, but not anymore):

XAML:

<Application ...
    Startup="Application_Startup">

Application:

public partial class App : Application
{
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        Window1 myMainWindow = new Window1();
        DialogWindow myDialogWindow = new DialogWindow();
        myDialogWindow.ShowDialog();
    }
}

Outcome: myDialogWindow is shown first. When it is closed, the Window1 is shown as expected. But as I close Window1 the application does not close at all.

7 Answers

I have the same issue when i need to disloag a login screen before my main window

  1. In you main window cunstructor add these lines

         Application.Current.MainWindow = this;
    
         Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
    
  2. Resolve the main window or just call var mainWindow = new MainWindow()

  3. Call the loginScreen.Show() or loginScreen.ShowDialog()

Related