I have an application that consumes a lot of time when loading a window. In the Window_load event, I read the status and name of some controls from the database. I want to make a splash screen that will end after the window has fully loaded.
I tried with this example http://www.codeproject.com/KB/dialog/wpf_animated_text_splash.aspx , but the splash screen closes before the main window is fully loaded and my mainwindow appears in white and is not fully loaded.
I'm new to wpf and I don't know how I can get a splash screen that stays on the screen until the main window loads completely.
Please give me an example.
My screensaver code:
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);
this.Dispatcher.Invoke(hideDelegate);
Thread.Sleep(6000);
this.Dispatcher.Invoke(showDelegate, "second data loading");
Thread.Sleep(6000);
this.Dispatcher.Invoke(hideDelegate);
Thread.Sleep(6000);
this.Dispatcher.Invoke(showDelegate, "last data loading");
Thread.Sleep(6000);
this.Dispatcher.Invoke(hideDelegate);
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);
}
}
And I will call this splash screen in my MainWindow constructor:
new SplashWindow().ShowDialog();
MainWindow Load , Splash , .
!