WPF showing dialog before main window

后端 未结 6 649
无人共我
无人共我 2020-12-03 02:49

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

相关标签:
6条回答
  • 2020-12-03 03:00

    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.

    0 讨论(0)
  • 2020-12-03 03:02

    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();
       }
    }
    
    0 讨论(0)
  • 2020-12-03 03:06

    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();
        }
    
    }
    
    0 讨论(0)
  • 2020-12-03 03:13

    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.

    0 讨论(0)
  • 2020-12-03 03:17

    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();
    
    0 讨论(0)
  • 2020-12-03 03:18

    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);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题