问题
WPF allows you to create a splash screen by adding an image file to your project and setting its build property to SplashScreen. The great thing about this feature (as opposed to rolling my own splash screen) is that the splash screen is shown immediately while the WPF app starts up (which can take a lot of time on older machines).
Is it possible to get a reference to this splash screen at runtime? There is the SplashScreen class, but, unfortunately, it does not have a static "Current" method or something like this.
Any kind of reference to the underlying splash screen window (Window instance or even just the underlying Windows API window handle) would be fine for me.
Background: There's a bug in WPF which causes the app to crash if the application's main window is closed while the splash screen is still visible. The bug won't be fixed, so I need to work around it by keeping my app "alive" until the splash screen has faded away. I currently do this by Thread.Sleep(2000)
-ing in my main window's Closing
event, but that's ugly and unreliable. I'd rather wait (only) until the splash screen window is gone.
回答1:
You could show and close a SplashScreen
explicitly yourself in the OnStartup
method or constructor of your App
class in App.xaml.cs
:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
SplashScreen splashScreen = new SplashScreen("splash.png");
splashScreen.Show(false);
//init your main window here...
splashScreen.Close(TimeSpan.FromSeconds(1));
}
Then you should be able to control the lifetime of both the splash screen and your application.
来源:https://stackoverflow.com/questions/46118045/how-to-i-get-a-reference-to-wpfs-splash-screen