I have an application that consumes a lot of time when the window loading. In the Window_load event, I read from the database the state and the name of some controls. I want to do a splash screen that will ends after the window will fully load.
I have 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 am beginner in wpf, and I don't know how can I have a splash screen which remain on the screen until the main window fully loads.
Please give me an example.
My Splash Screen 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);
//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);
}
}
And this splash screen I will call in my MainWindow constructor:
new SplashWindow().ShowDialog();
But my MainWindow Load function will run after the Splash Window will finish to be showed.
Thank you!
If you use the built-in SplashScreen
class, you can call Show(false)
to specify that you will be responsible for closing the splash screen. You can then use the Close()
method to close it.
Note that the SplashScreen
class only supports displaying a static image. It does this for very good reasons though - to get the splash screen up in front of your user as soon as possible.
Code would look something like this:
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));
}
}
For the best results, you can use two-phase approach to SplashScreen:
Phase 1. Display static splash screen even before .NET code starts loading.
Static image loads using native code yet before .NET is initialized. Very effective to inform the user as soon as possible. There is a special way to achieve this.
Phase 2. Display custom form once .NET is loaded.
Design your custom form looking identically to your static splash screen when initially displayed. Showing the form will automatically fade-out static splash screen shown in Phase 1 (it is done by WPF) and since then you are free to display loading progress of your application. Hide this form (which can be always-on-top window) when your main window finishes loading its data.
If your splash screen is just an image - add the image to the project and set it's Build Action property to 'SplashScreen'. The framework will handle the rest. (VS2008 sp1 or later).
If you need the splash screen to differ (maybe displaying a version number) this approach wont work. If you want this take a look at the SplashScreen class that gives a little more flexibility.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With