SplashScreen类为WPF应用程序提供启动屏幕。有两种添加方法:
方法一:设置图片属性
- 添加启动图片到项目中
- 设置图片属性的Build Action为SplashScreen
方法二:编写代码
在App.xaml.cs中重写OnStartUp方法:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace WPFOSGMDI
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
SplashScreen splashScreen = new SplashScreen("/Resources/splashscreen.png");
splashScreen.Show(false);
//上面Show()方法中设置为true时,程序启动完成后启动图片就会自动关闭,
//设置为false时,启动图片不会自动关闭,需要使用下面一句设置显示时间,例如23s
splashScreen.Close(new TimeSpan(0, 0, 23));
base.OnStartup(e);
}
}
}
注意:图片的 生成操作属性 设置为 “resource”
路径设置为 ”/Resources/splashscreen.png“
Resources 为存放文件的文件夹。