如何获得对 WPF 启动画面的引用? [英] How to I get a reference to WPF's splash screen?

查看:28
本文介绍了如何获得对 WPF 启动画面的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF 允许您通过 将图像文件添加到您的项目并将其构建属性设置为 SplashScreen.这个功能的好处(与滚动我自己的启动画面相反)是启动画面在 WPF 应用程序启动时立即显示(这在旧机器上可能需要很多时间).

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

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

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.

对底层启动屏幕窗口的任何类型的引用 (Window 实例 甚至只是底层的 Windows API 窗口句柄)对我来说都很好.

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.

背景:有一个WPF 中的错误,如果应用程序主窗口关闭,而初始屏幕仍然可见.错误 不会被修复,所以我需要通过保持我的应用程序活动"直到闪屏消失来解决它.我目前通过 Thread.Sleep(2000)-ing 在我的主窗口的 Closing 事件中执行此操作,但这很丑陋且不可靠.我宁愿(仅)等到启动画面窗口消失.

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.

推荐答案

您可以自己在 OnStartup 方法或 构造函数中显式地显示和关闭 SplashScreenApp.xaml.cs 中的 >App 类:

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.

这篇关于如何获得对 WPF 启动画面的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆