![]() |
Platforms, Frameworks & Libraries »
Windows Presentation Foundation »
Applications
Beginner
License: The Code Project Open License (CPOL)
Implement Splash Screen with WPFBy Andy LangShow a custom splash screen with loading messages |
C#, Windows, .NET
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
Now almost all applications have a splash screen. This screen will be shown when the application starts, after the main window of the application is shown, this screen will be closed.
WPF has provided an easy way to implement splash screen. Using this way, we should add an image as resource to the project, and set its BuildAction property to “SplashScreen”. This feature is only supported on .NET Framework 3.5. When the application starts, the image will be shown at first as the splash screen. I know this way is implemented via WinForm technology.
The above method is easy, but it is too simple and doesn’t meet my requirements. My requirements of splash screen are:
Normally a splash screen has the below features:
To meet the above requirements, my design is as below:

App class inherits from Application class and provides the application entry.
When entering the Main method, initialize the splash screen and show it by calling the Splasher class, and then run the application, show the main window.
/// <summary>
///
/// </summary>
class App: Application
{
/// <summary>
///
/// </summary>
[STAThread ( )]
static void Main ( )
{
Splasher.Splash = new SplashScreen ( );
Splasher.ShowSplash ( );
// Simulate application loading
for ( int i = 0; i < 5000; i++ )
{
MessageListener.Instance.ReceiveMessage
( string.Format ( "Load module {0}", i ) );
Thread.Sleep ( 1 );
}
new App ( );
}
/// <summary>
///
/// </summary>
public App ( )
{
StartupUri = new System.Uri ( "MainWindow.xaml", UriKind.Relative );
Run ( );
}
}
Application’s main window, it’s just a normal window.
Splash screen window, it’s just a normal window, this window has the special features that a splash screen should have. I have mentioned it above.
A helper class to show and close splash screen window, the splash screen window should be initialized by the application. With this class, I don't care which window is the splash screen, and it’s reusable.
/// <summary>
/// Helper to show or close given splash window
/// </summary>
public static class Splasher
{
/// <summary>
///
/// </summary>
private static Window mSplash;
/// <summary>
/// Get or set the splash screen window
/// </summary>
public static Window Splash
{
get
{
return mSplash;
}
set
{
mSplash = value;
}
}
/// <summary>
/// Show splash screen
/// </summary>
public static void ShowSplash ( )
{
if ( mSplash != null )
{
mSplash.Show ( );
}
}
/// <summary>
/// Close splash screen
/// </summary>
public static void CloseSplash ( )
{
if ( mSplash != null )
{
mSplash.Close ( );
if ( mSplash is IDisposable )
( mSplash as IDisposable ).Dispose ( );
}
}
}
This is a helper class, implement DoEvents method like System.Windows.Forms.Application’s DoEvents method, if there is no such function, the loading message of the splash will not be refreshed correctly. The code is shown below:
/// <summary>
/// Simulate Application.DoEvents function of
/// <see cref=" System.Windows.Forms.Application"/> class.
/// </summary>
[SecurityPermissionAttribute ( SecurityAction.Demand,
Flags = SecurityPermissionFlag.UnmanagedCode )]
public static void DoEvents ( )
{
DispatcherFrame frame = new DispatcherFrame ( );
Dispatcher.CurrentDispatcher.BeginInvoke ( DispatcherPriority.Background,
new DispatcherOperationCallback ( ExitFrames ), frame );
try
{
Dispatcher.PushFrame ( frame );
}
catch ( InvalidOperationException )
{
}
}
This class inherit from DependencyObject, WPF’s UI element can easily bind to its property and update correctly. This class just receives a string message. To show the loading progress message in the splash screen, I add a TextBlock element to the window, and bind its Text property to listener’s Message property, and when the application is loading, I write messages to the listener.
The final splash screen looks like below. I define the window with a WPF window, the Label at the bottom of the window shows the loading tip messages (progress) of the application.
A very simple main window.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 23 Jul 2009 Editor: Deeksha Shenoy |
Copyright 2009 by Andy Lang Everything else Copyright © CodeProject, 1999-2010 Web21 | Advertise on the Code Project |