Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF showing dialog before main window

Tags:

dialog

wpf

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.

like image 535
Ciantic Avatar asked Oct 08 '09 19:10

Ciantic


1 Answers

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);         }     } } 
like image 117
Gleno Avatar answered Oct 26 '22 23:10

Gleno