Is there a way to show a splash screen for WPF application?

sokolovic picture sokolovic · Oct 12, 2010 · Viewed 25.3k times · Source

Dupe: WPF animated splash screen

I would like to show a splash screen for my WPF application. what I want to do is to show it while I load dictionary from a file (it takes about 5-6 seconds to load). Is there a way to achieve this in WPF? I would appreciate some tutorial, since this is a little bit more complicated then other questions I posted.

Answer

Wonko the Sane picture Wonko the Sane · Oct 12, 2010

A SplashScreen is really just another Window with no border, and it is not resizable (nor can you interact with it in any way). You'd probably want to hide it from the task bar, center it on the screen, etc. Play around with various settings until you get the effect that you want.

Here's a quick one I whipped up in about 5 minutes to prove the theory:

<Window x:Class="MyWhateverApp.MySplashScreen"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        ShowInTaskbar="False" 
        ResizeMode="NoResize" 
        WindowStartupLocation="CenterScreen"
        WindowStyle="None" 
        Background="Transparent" 
        AllowsTransparency="True"
        Title="Sandbox Splash Screen" 
        SizeToContent="Width" 
        Topmost="True" 
        Height="{Binding RelativeSource={RelativeSource Self}, 
                         Path=ActualWidth}">

    <Border CornerRadius="8" Margin="15">
        <Border.Background>
            <ImageBrush ImageSource="Resources\sandtexture.jpeg" 
                        Stretch="Fill" />
        </Border.Background>
        <Border.Effect>
            <DropShadowEffect Color="#894F3B" 
                              BlurRadius="10" 
                              Opacity="0.75" 
                              ShadowDepth="15" />
        </Border.Effect>

        <TextBlock FontSize="40"
                   FontFamily="Bauhaus 93"
                   Foreground="White"
                   Margin="10"
                   VerticalAlignment="Center"
                   HorizontalAlignment="Center"
                   Text="WPF 3.5 Sandbox">
            <TextBlock.Effect>
                <DropShadowEffect Color="Black" />
            </TextBlock.Effect>
        </TextBlock>        
    </Border>
</Window>

Next, modify your App.xaml file to remove the startup window, and instead raise the Startup event:

<Application x:Class="MyWhateverApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="Application_Startup">
    <Application.Resources>

    </Application.Resources>
</Application>

And in the code-behind, handle the Application_Startup event in whatever way you think is best. For example:

Window1 mainWindow = null;

private void Application_Startup(object sender, StartupEventArgs e)
{
    MySplashScreen splash = new MySplashScreen();
    splash.Show();
    mainWindow = new Window1();
    mainWindow.Show();
    splash.Close();
}