如何获得对 WPF 启动画面的引用?

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

WPF 允许您通过 adding an image file to your project and setting its build property to SplashScreen 创建启动画面。此功能的优点(与滚动我自己的启动画面相反)是启动画面 立即显示 当 WPF 应用程序启动时(这可能会花费很多时间旧机)。

是否可以在运行时获取对此启动画面的引用?有 SplashScreen class,但不幸的是,它没有静态 "Current" 方法或类似的方法。

对底层启动画面 window(Window instance 甚至只是底层 Windows API window 句柄的任何类型的引用都可以对我来说。

背景: 有一个 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,所以我需要通过保持我的应用 "alive" 直到启动画面消失来解决它。我目前通过 Thread.Sleep(2000)-ing 在我的主要 window 的 Closing 事件中执行此操作,但这很丑陋且不可靠。我宁愿(只)等待启动画面 window 消失。

您可以在 OnStartup 方法或 App.xaml.cs 中 class 的 OnStartup 方法或构造函数中显式显示和关闭 SplashScreen:

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

那么您应该能够控制启动画面和您的应用程序的生命周期。