带有GIF动画的初始屏幕在WPF C#中不是动画 [英] Splash Screen with Animated GIF is not animating in WPF C#

查看:23
本文介绍了带有GIF动画的初始屏幕在WPF C#中不是动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况是这样的。我当前正在开发一个C#WPF桌面应用程序,我想在启动该程序时添加一个闪屏。

但我没有使用SplashScreen类。我在同名应用程序的基础上使用了一个额外的WPF窗口类,我称这个类为Splashy。

我的初始屏幕有一个GIF动画图像,当在下面Splashy类的WebBrowser控件上设置动画时,该图像效果很好...

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

...并且Splashy类本身在App.XAML中被初始化为下面的StartupUri。

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

但该方案的问题是闪屏仅挂起,并且不会进入主程序本身。

所以我尝试了一种不同的方法。一个使用代码。

当我将以下代码实现到闪屏,然后将其实现到我的程序以运行闪屏,然后显示主屏幕时,闪屏执行了我想要做的事情但它没有像我想要的那样动画

下面是醒目的类代码

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

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

和下面是实现plashScreen的程序方法。

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

    public LoginScreen()
    {

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

        // home screen 
    }
}

如有任何提示和帮助,我们将不胜感激。

推荐答案

我会尝试这样做。创建处理所有这些逻辑的Bootstrapper类。

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();
    }
}

App.xaml中删除StartupUri,这样就不会先创建您的MainWindow

<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">

变成...

<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">

然后,在App.xaml.cs中,覆盖启动,以便它创建Bootstrapper类的新实例。

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

这篇关于带有GIF动画的初始屏幕在WPF C#中不是动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆