需求:打开WPF项目后,展示3秒钟产品Logo后,进入主界面MainWindow。(类似于安卓应用打开时的闪屏页SplashPage)
思路:在进入MainWindow后新建一个Window窗体,窗体的背景设置为Logo图片,窗体设置为最大化、Z轴置顶、不可调整宽高、不显示状态栏。设置一个DispatcherTimer定时器,3秒后关闭窗体。
MainWindow.xaml.cs 后台代码的主要逻辑:
private Window window;
public MainWindow()
InitializeComponent();
this.Loaded += new RoutedEventHandler(ShowLoginWindow);
private void ShowLoginWindow(object sender, RoutedEventArgs e)
window = new Window();
window.Topmost = true;
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
window.WindowState = WindowState.Maximized;
window.ResizeMode = ResizeMode.NoResize;
window.WindowStyle = WindowStyle.None;
ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("pack://application:,,,/HomeDecorationPSD;component/Presentation/Resources/Images/app_logo.jpg"));
window.Background = brush;
StartCloseTimer();
window.ShowDialog();
private void StartCloseTimer()
DispatcherTimer timer = new DispatcherTimer();
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)
window.Close();
配置文件App.config 添加内容
<appSettings>
<add key="LOGO_WINDOW_AUTO_CLOSE_TIMER" value="3" />
</appSettings>
重要参考:
http://stackoverflow.com/questions/11719283/how-to-close-auto-hide-wpf-window-after-10-sec-using-timer