47

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 7

87

Here's the full solution that worked for me:

In App.xaml, I remove the StartupUri stuff, and add a Startup handler:

<Application x:Class="MyNamespace.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="ApplicationStart">
</Application>

In App.xaml.cs, I define the handler as follows:

public partial class App
{
    private void ApplicationStart(object sender, StartupEventArgs e)
    {
        //Disable shutdown when the dialog closes
        Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;

        var dialog = new DialogWindow();

        if (dialog.ShowDialog() == true)
        {
            var mainWindow = new MainWindow(dialog.Data);
            //Re-enable normal shutdown mode.
            Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
            Current.MainWindow = mainWindow;
            mainWindow.Show();
        }
        else
        {
            MessageBox.Show("Unable to load data.", "Error", MessageBoxButton.OK);
            Current.Shutdown(-1);
        }
    }
}
2
  • 3
    It is not necessary to remove StartupUri to achieve the same outcome. The important bit is to change shutdown mode before the dialog, and then to change it back after the dialog. Where we can we should aim to use code behind to augment the designer only when it is too complex or inefficient to express in xaml. In this case one property in Xaml that we all expect is better than 2 lines hidden inside conditional logic. Commented Oct 9, 2016 at 22:53
  • Just to notice that default Current.ShutdownMode is ShutdownMode.OnLastWindowClose for those how will just copy and paste the code above.
    – vinsa
    Commented Jun 26, 2018 at 21:09
6

Okay apologizes, here is the solution:

My original question worked almost, only one thing to add, remove the StartupUri from the Application XAML and after that add the Show to main window.

That is:

<Application x:Class="DialogBeforeMainWindow.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Startup="Application_Startup">

Above, StartupUri removed.

Add myMainWindow.Show() too:

public partial class App : Application
{

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        Window1 myMainWindow = new Window1();
        DialogWindow myDialogWindow = new DialogWindow();
        myDialogWindow.ShowDialog();
        myMainWindow.Show();
    }

}
2
  • 2
    This did not work for me, the application shutdown after DialogWindow was closed.
    – Gleno
    Commented Feb 21, 2012 at 2:24
  • Didn't work for me either - OK = Main shown fine. Cancel = hangs, i.e. doesn't close. Answer needs reworking.
    – Bertie
    Commented Jul 9, 2015 at 15:58
5

WPF sets App.Current.MainWindow to the first window opened. If you have control over the secondary window constructor, just set App.Current.MainWindow = Null there. Once your main window is constructed, it will be assigned to the App.Current.MainWindow property as expected without any intervention.

public partial class TraceWindow : Window
{
    public TraceWindow()
    {
        InitializeComponent();
        if (App.Current.MainWindow == this)
        {
            App.Current.MainWindow = null;
        }
    }
}

If you don't have access, you can still set MainWindow within the main window's constructor.

4

If you put Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown; into the constructor of the dialog, and add

protected override void OnClosed(EventArgs e) {
    base.OnClosed(e);
    Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
}

into the dialog class, you don't need to worry about making any changes to the default behaviour of the application. This works great if you want to just snap a login screen into an already-existing app without tweaking the startup procedures.

1
  • Without doubt the easiest of the solutions to implement. It might not be the purists mvvm (ie absolutely no code in the code behind) but I don't think that adding this little bit makes much if any difference and does at least have the great advantage to being retrofit as well. Commented Oct 28, 2014 at 13:35
2

So you want to show one window, then another, but close down the app when that window is closed? You may need to set the ShutdownMode to OnMainWindowClose and set the MainWindow to Window1, along the lines ok:

Window1 myMainWindow = new Window1();
Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
Application.Current.MainWindow = myMainWindow;
DialogWindow myDialogWindow = new DialogWindow();
myDialogWindow.ShowDialog();
2
  • Could you paste whole app class? (That didn't work (application never closes), nor did the modified version I tried) Also if you look at the link I pasted, it says the MainWindow is set already...
    – Ciantic
    Commented Oct 8, 2009 at 19:49
  • Easy debugging confirmed the MainWindow, and anything I put in ShutdownMode doesn't matter.
    – Ciantic
    Commented Oct 8, 2009 at 20:08
0

here, do it like this. this will actaully change your main window and will work properly w/o having to change settings of your application object.

make sure to remove the event handler for application startup and to set your StartupUri in your app.xaml file.

public partial class App : Application
{
   bool init = false;
   protected override void OnActivated(EventArgs e)
   {
      base.OnActivated(e);
      if (!init)
      {
         this.MainWindow.Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);
         init = true;
      }
   }

   void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
   {
      Window toClose = this.MainWindow;
      this.MainWindow = new Window2();
      this.MainWindow.Show();
   }
}
6
  • I'll give it a try, but you do realize that, this can't be the "right" way to do it. There have to be some good way to do this. Every place I search the OnStartup is populated or overridden... it even gets the command line arguments. Also, I tried to create "Run" function to my app class, and if I do so, nothing appears unless I put base.Run(...). So that might be a something to look after.
    – Ciantic
    Commented Oct 8, 2009 at 20:21
  • you can still override the OnStartup to handle your runtime args and such. why can't this be the "right" way to do it? this works wonderfully.
    – Muad'Dib
    Commented Oct 8, 2009 at 20:30
  • It is not elegant, this thing should be easy. As it was, but apologizes to you also, I messed up with my question the "..." in application tag was too much.
    – Ciantic
    Commented Oct 8, 2009 at 20:39
  • Yes, there SHOULD be a more elegant way to do this, but i have not been able to find it.
    – Muad'Dib
    Commented Oct 8, 2009 at 20:41
  • Chris, I answered this myself, it has 1 point and that is the "Right" I believe so.
    – Ciantic
    Commented Oct 8, 2009 at 20:48
0

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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.