Show window in Application_Startup. Wpf

I have this small piece of code.

private void Application_Startup(object sender, StartupEventArgs e)
        {
            WndAbout ab = new WndAbout();
            ab.Show();
        }

And you want to show a window or dialog when the application starts, before other modules are loaded.

But! When I close the window shown, the main window, which starts later, also closes!

What am I doing wrong? I tried to make Showdialog () - the same situation arose.

+3
source share
2 answers

The problem you are facing is that WPF manages the shutdown.

You can change the shutdown behavior through ...

Application.Current.ShutdownMode

... property. Change it to approriate:

Application.Current.ShutdownMode = System.Windows.ShutdownMode.OnLastWindowClose;

it will help.

Another way is to manually install MainWindow-property in the second window.

, splashscreen, .net 3.51. , , , .

, SplashScreen


, , . ShutdownMode -property.

, , Window, . , - ( WPF). (, ). , , . , MainWindow, .

+6
private void Application_Startup(object sender, StartupEventArgs e) 
{ 
  ShutdownMode mode = this.ShutdownMode;
  this.ShutdownMode = System.Windows.ShutdownMode.OnExplicitShutdown;
  YourLogonWnd logon = new YourLogonWnd();
  logon.ShowDialog();
  if (!logon.DialogResult.HasValue || !logon.DialogResult.Value)
    this.Shutdown();
  else
    this.ShutdownMode = mode;
}

, "OnMainWindowClose" .

+2

Source: https://habr.com/ru/post/1755998/


All Articles