I have a splash screen that I would like to fade in, while my application goes off and does something, then once complete, if a certain amount of time has elapsed, closes the splash screen and loads the main screen.
The issue Im getting is that while I am looping to check if the amount of time has elapsed, it does not seem to be processing my storyboard to fade in the splash screen.
After reading around I guess I should be starting the splash screen on a different thread?
Heres the code:
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
MySplashScreen splash = new MySplashScreen();
splash.Show();
Stopwatch sw = new Stopwatch();
sw.Start();
while (sw.Elapsed.TotalSeconds < 10)
{
}
splash.Close();
MainWindow mw = new MainWindow();
mw.Show();
}
}
The issue is when I add the animation. Its like its not applying any processor to the fade because its stuck in the while loop.
<Window x:Class="Splash_Demo.MySplashScreen"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="550" Width="900" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None" ShowInTaskbar="False" Background="Transparent" AllowsTransparency="True" Opacity="0">
<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="00:00:03" Storyboard.TargetProperty="Opacity" To="1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
<Canvas Height="498" Width="839">
<Canvas.Background>
<ImageBrush ImageSource="C:\Users\Ash\Downloads\XactSplash.png"/>
</Canvas.Background>
<Label Canvas.Left="291" FontFamily="Algerian" Canvas.Top="413" Name="Customer" Height="43" Width="185" HorizontalContentAlignment="Left" VerticalContentAlignment="Center"/>
<Image Canvas.Left="500" Canvas.Top="165" Height="164" Name="image1" Stretch="Fill" Width="211" Source="C:\Users\Ash\Downloads\Zerix.bmp" />
<Label Canvas.Left="191" Canvas.Top="376" FontSize="8" Content="Label" Height="19" Name="lblYear" Width="30" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" />
</Canvas>
</Window>
You’re busy-waiting, causing the UI thread to block, and taking a lot of CPU in the process. Instead, set up a timer in your Application_Startup, and open the main window when it expires.