Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 622
sokolovic Avatar asked Oct 12 '10 11:10

sokolovic


People also ask

How do I display a WPF window?

When a Window is created at run-time using the Window object, it is not visible by default. To make it visible, we can use Show or ShowDialog method. Show method of Window class is responsible for displaying a window.

What is splash screen in WPF?

The image that is displayed before the application starts is called splash screen. One more use of a splash screen is, to show user some action while your application is loading initial required data. It is very easy to add a splash screen to a WPF application.

Is splash screen the same as loading screen?

A splash screen, also known as a launch screen, is the first screen that a user sees when opening your app, and it stays visible while the app is loading. You can control when the splash screen disappears by using the native SplashScreen API.


1 Answers

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();
}
like image 175
Wonko the Sane Avatar answered Oct 16 '22 01:10

Wonko the Sane