How to I get a reference to WPF's splash screen?

做~自己de王妃 提交于 2019-12-12 04:04:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!