Requirements: After opening the WPF project, after displaying the product logo for 3 seconds, enter the main window MainWindow. (Similar to SplashPage when the Android app is opened)
Idea: After entering MainWindow, create a new Window form, the background of the form is set to Logo picture, the form is set to maximize, Z axis is set to the top, the width and height cannot be adjusted, and the status bar is not displayed. Set a DispatcherTimer timer, close the window after 3 seconds.
Main logic of MainWindow.xaml.cs background code:
private Window window;
public MainWindow()
{
InitializeComponent();
// 界面加载完成后触发的事件
this.Loaded += new RoutedEventHandler(ShowLoginWindow);
}
private void ShowLoginWindow(object sender, RoutedEventArgs e)
{
// 弹窗显示Logo 3秒后关闭该弹窗自动关闭
window = new Window();
// 经测试,以下设置无法做到窗体跟屏幕一样大,窗体四周会留下几个像素的边距
//double screenHeight = SystemParameters.FullPrimaryScreenHeight;
//double screenWidth = SystemParameters.FullPrimaryScreenWidth;
//window.Width = screenWidth;
//window.Height = screenHeight;
//window.Top = (screenHeight - window.Height) / 2;
//window.Left = (screenWidth - window.Width) / 2;
window.Topmost = true; // 置顶
window.WindowStartupLocation = WindowStartupLocation.CenterScreen; // 屏幕中心
window.WindowState = WindowState.Maximized; // 最大化
window.ResizeMode = ResizeMode.NoResize; // 不能调宽高
window.WindowStyle = WindowStyle.None; // 无窗体样式,即可不显示状态栏
// 背景是Logo图片
ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("pack://application:,,,/HomeDecorationPSD;component/Presentation/Resources/Images/app_logo.jpg")); // 这是绝对路径
window.Background = brush;
// 设置窗体3秒后自动关闭
StartCloseTimer();
window.ShowDialog();
}
private void StartCloseTimer()
{
DispatcherTimer timer = new DispatcherTimer();
//timer.Interval = TimeSpan.FromSeconds(3); // 3秒
// 为了方便测试,可以把这个秒数写到App.config配置文件中
double t = double.Parse(ConfigurationManager.AppSettings["LOGO_WINDOW_AUTO_CLOSE_TIMER"]);
timer.Tick += TimerTick; // 注册计时器到点后触发的回调
timer.Start();
}
private void TimerTick(object sender, EventArgs e)
{
DispatcherTimer timer = (DispatcherTimer)sender;
timer.Stop();
timer.Tick -= TimerTick; // 取消注册
window.Close();
}
private void CloseLogoWindow(object state)
{
// 关闭Logo窗体
window.Close();
}
Configuration file App.config add content
<appSettings>
...
<!-- 打开软件时Logo弹窗自动关闭倒计时秒数 类似闪屏页SplashPage -->
<add key="LOGO_WINDOW_AUTO_CLOSE_TIMER" value="3" />
</appSettings>
Important reference: