App xaml assumes the first window instantiated is the main window (showdialog is ignored), I need to show multiple windows

StackOverflow https://stackoverflow.com/questions/4199932

Question

I have the following code in my App.xaml.cs

private void App_Start(object sender, StartupEventArgs e)
{
  if ( CompletedInstall())
  {
    //using show to allow for pacifier if loading is slow
    var manager = new WINServiceConfig();
    MainWindow = manager;
    manager.ShowDialog();
  }
}

private bool CompletedInstall()
{
    var window = new Initialize();
    window.ShowDialog();
    return window.DoLaunchManager;
}

and the following in the App.xaml

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

When i comment out the line that checks CompletedInstall() the manager.ShowDialog() works fine, and my configuration window shows. When CompletedInstall() is called the call to manager.ShowDialog() returns right away without displaying the window. I added the main window on the assumption that somewhere along the line someone decided an app should only show one window.

I found a workaround by setting the main window before calling CompletedInstall

        private void App_Start(object sender, StartupEventArgs e)
        {
          var manager = new WINServiceConfig();
          MainWindow = manager;

          if (CompletedInstall())
          {
            manager.ShowDialog();
          }

but this forces me to develop WINServiceConfig (specifically the constructor) based on its use, because it cannot count on the prerequisites being completed. This is bad form. What else can i do to get around this problem?

Dummy window? That can't be the best answer. Can it??

Was it helpful?

Solution

You should set the ShutdownMode to OnExplicitShutdown (at least while showing the initial dialog).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top