tags:

views:

467

answers:

3

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.

+1  A: 

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();
Steven Robbins
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
Easy debugging confirmed the MainWindow, and anything I put in ShutdownMode doesn't matter.
Ciantic
A: 

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();
   }
}
Muad'Dib
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
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
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
Yes, there SHOULD be a more elegant way to do this, but i have not been able to find it.
Muad'Dib
Chris, I answered this myself, it has 1 point and that is the "Right" I believe so.
Ciantic
does that close the app when you close the window?
Muad'Dib
+3  A: 

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

}
Ciantic