我正在制作一个自定义启动画面(因为标准启动画面不符合我的需求)。但是有一个选项我想要实现——自动关闭。但是为了实现它,我需要了解常见的
那么,是否有任何事件可以向启动画面发送消息,告诉它应该关闭呢?至少常见的启动画面使用什么事件?
SplashScreen
是如何选择关闭时机的。那么,是否有任何事件可以向启动画面发送消息,告诉它应该关闭呢?至少常见的启动画面使用什么事件?
SplashScreen
是如何选择关闭时机的。与其将Build Action设置为Splash Screen的图像文件,您可以通过在应用程序的OnStartup事件处理程序中创建并显示自己的启动屏幕来更好地控制启动屏幕。SplashScreen的show方法有一个参数,可以阻止其自动关闭,然后您可以使用Close方法告诉它何时关闭:
首先从App.xaml中删除StartupUri标签:
<Application x:Class="Splash_Screen.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
</Application.Resources>
</Application>
将图像文件的构建操作(Build Action)
更改为资源(Resource)
然后在OnStartup
事件处理程序中创建并显示启动画面:
public partial class App : Application
{
private const int MINIMUM_SPLASH_TIME = 1500; // Miliseconds
private const int SPLASH_FADE_TIME = 500; // Miliseconds
protected override void OnStartup(StartupEventArgs e)
{
// Step 1 - Load the splash screen
SplashScreen splash = new SplashScreen("splash.png");
splash.Show(false, true);
// Step 2 - Start a stop watch
Stopwatch timer = new Stopwatch();
timer.Start();
// Step 3 - Load your windows but don't show it yet
base.OnStartup(e);
MainWindow main = new MainWindow();
// Step 4 - Make sure that the splash screen lasts at least two seconds
timer.Stop();
int remainingTimeToShowSplash = MINIMUM_SPLASH_TIME - (int)timer.ElapsedMilliseconds;
if (remainingTimeToShowSplash > 0)
Thread.Sleep(remainingTimeToShowSplash);
// Step 5 - show the page
splash.Close(TimeSpan.FromMilliseconds(SPLASH_FADE_TIME));
main.Show();
}
}
ContentRendered
事件)。如果您愿意深入了解,这里是SplashScreen
的源代码。 - SinatrSplashScreen
类(我也承认我之前没有听说过)。 - anaximander