我有一个应用程序,在窗口加载时消耗大量时间.
在Window_load事件中,我从数据库中读取了一些控件的状态和名称.
我想做一个闪屏,它会在窗口完全加载后结束.
在Window_load事件中,我从数据库中读取了一些控件的状态和名称.
我想做一个闪屏,它会在窗口完全加载后结束.
我已尝试使用此示例http://www.codeproject.com/KB/dialog/wpf_animated_text_splash.aspx,但在主窗口完全加载之前启动屏幕关闭,我的主窗口显示为白色且未完全加载.
我是wpf的初学者,我不知道在主窗口完全加载之前我怎么能有一个保持在屏幕上的闪屏.
请举个例子.
我的启动画面代码:
public partial class SplashWindow : Window { Thread loadingThread; Storyboard Showboard; Storyboard Hideboard; private delegate void ShowDelegate(string txt); private delegate void HideDelegate(); ShowDelegate showDelegate; HideDelegate hideDelegate; public SplashWindow() { InitializeComponent(); showDelegate = new ShowDelegate(this.showText); hideDelegate = new HideDelegate(this.hideText); Showboard = this.Resources["showStoryBoard"] as Storyboard; Hideboard = this.Resources["HideStoryBoard"] as Storyboard; } private void Window_Loaded(object sender,RoutedEventArgs e) { loadingThread = new Thread(load); loadingThread.Start(); } private void load() { Thread.Sleep(6000); this.Dispatcher.Invoke(showDelegate,"first data to loading"); Thread.Sleep(6000); //load data this.Dispatcher.Invoke(hideDelegate); Thread.Sleep(6000); this.Dispatcher.Invoke(showDelegate,"second data loading"); Thread.Sleep(6000); //load data this.Dispatcher.Invoke(hideDelegate); Thread.Sleep(6000); this.Dispatcher.Invoke(showDelegate,"last data loading"); Thread.Sleep(6000); //load data this.Dispatcher.Invoke(hideDelegate); //close the window Thread.Sleep(6000); this.Dispatcher.Invoke(DispatcherPriority.Normal,(Action)delegate() { Close(); }); } private void showText(string txt) { txtLoading.Text = txt; BeginStoryboard(Showboard); } private void hideText() { BeginStoryboard(Hideboard); } }
new SplashWindow().ShowDialog();
但是我的MainWindow加载功能将在Splash Window完成后显示.
谢谢!
解决方法
如果使用内置的
SplashScreen
类,则可以调用
Show(false)
来指定您将负责关闭启动画面.然后,您可以使用
Close()
方法关闭它.
请注意,SplashScreen类仅支持显示静态图像.这样做有很好的理由 – 尽快将闪屏放在用户面前.
代码看起来像这样:
static class Entry { static void Main(string[] args) { var splashScreen = new SplashScreen("path/to/your/image.png"); splashScreen.Show(false); InitializeLogging(); InitializeServices(); InitializeUserInterface(); InitializeWhateverElseYouNeed(); splashScreen.Close(TimeSpan.FromSeconds(1)); } }