Splash Screen with Animated GIF is not animating in WPF C#

此生再无相见时 提交于 2021-02-08 23:43:16

问题


So here is the situation. I am currently working on a C# WPF Desktop Application and I wanted to add a Splash Screen when starting the program.

But I am not using the SplashScreen class. I am using an additional WPF windows class on top of the app of the same name, and I call that class Splashy.

My splash screen has an animated GIF image that works well when animated on the WebBrowser control of the Splashy class below...

<Image gif:ImageBehavior.AnimatedSource="Images/FSIntro.gif" Margin="10,1,0,10">
    <Image.OpacityMask>
        <ImageBrush ImageSource="Images/FSIntro.gif"/>
    </Image.OpacityMask>
</Image>

...and the Splashy class itself is initialized as a StartupUri below in the App.XAML.

<Application
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         ...  ...
         ... (some text) ...
         ...  ...
         StartupUri="Splashy.xaml">
<Application.Resources>

But the problem with that scenario is the splash screen just hangs and does not go into the main program itself.

So I tried a different approach. One using code.

And when I implement the following code to the Splash Screen and then implement it to my program to run the splash screen and then display the home screen, the splash screen does what I want to do but it does not animate as I would want.

Below is the Splashy class code

public partial class Splashy : Window
{
    public Intro()
    {
        InitializeComponent();
    }

    public void Outro()
    {
        this.Close();
    }
}

And below is the program's method where the splashScreen is being implemented.

public partial class LoginScreen : Window
{
    Splashy splash = new Splashy();

    public LoginScreen()
    {

        splash.Show();
        Thread.Sleep(5000);
        splash.Outro();

        // home screen 
    }
}

Any pointers and help would be greatly appreciated.


回答1:


I would try something like this instead. Create a Bootstrapper class that handles all this logic.

public class BootStrapper
{
    SplashScreen Splash = new SplashScreen();
    MainWindow MainWindow = new MainWindow();

    public void Start()
    {
        var timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(2);
        timer.Tick += (s, e) =>
        {
            Splash.Close();
            MainWindow.Show();
            timer.Stop();
        };

        timer.Start();
        Splash.Show();
    }
}

Remove the StartupUri from App.xaml so that your MainWindow isn't created first.

<Application x:Class="StackOverflow.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:StackOverflow"
         StartupUri="MainWindow.xaml">

becomes...

<Application x:Class="StackOverflow.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:StackOverflow">

Then, in the App.xaml.cs, override the startup so that it creates a new instance of your Bootstrapper class.

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        var bootStrapper = new BootStrapper();
        bootStrapper.Start();
        base.OnStartup(e);
    }
}


来源:https://stackoverflow.com/questions/50664221/splash-screen-with-animated-gif-is-not-animating-in-wpf-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!