WPF showing dialog before main window WPF showing dialog before main window wpf wpf

WPF showing dialog before main window


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


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


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.