WPF C#中带有动画GIF的启动画面无法播放

4

所以现在的情况是,我正在开发一个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>

但这种情况的问题在于启动画面卡住了,无法进入主程序本身。
所以我尝试了一种不同的方法,使用代码。
当我将以下代码实现到启动画面中,并将其实现到我的程序中以运行启动画面并显示主屏幕时,启动画面会按照我想要的方式执行,但它不会像我希望的那样动画化
下面是Splashy类代码。
public partial class Splashy : Window
{
    public Intro()
    {
        InitializeComponent();
    }

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

以下是程序实现启动画面的方法。
public partial class LoginScreen : Window
{
    Splashy splash = new Splashy();

    public LoginScreen()
    {

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

        // home screen 
    }
}

任何建议和帮助都将不胜感激。

很可能是因为您正在阻塞UI线程,导致出现此问题? - Ralt
程序员通常使用闪屏来掩盖其用户界面线程无响应的事实。 此时它正在繁忙地初始化自己。 “无响应”包括无法获取GIF动画。这不是您应该考虑修复的东西,因为要正确完成这项工作相当困难,并且在您这样做时可能不再需要闪屏了 :) 保持简单。 - Hans Passant
@Ross Chris 请在你有时间的时候标记为正确。 - Ralt
2个回答

2
我会尝试做出类似这样的改动。创建一个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);
    }
}

非常感谢你,Ralt!很抱歉回复晚了,但我真的很感激你在这里的帮助。这种方法节省了我的时间和生命。 :) - Ross Chris

0

你好,如果你想在WPF应用程序加载时间长的情况下加载一个启动屏幕,请在创建好你的动画窗口后,保持App.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"
     StartupUri="MainWindow.xaml">

在App.Xaml.cs类中以这种形式插入您的屏幕:

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{   
    public static readonly BootstrapScreen BoostrapScreen = new BootstrapScreen();
    public App()
    {
        this.Startup += (sender, e) =>
        {
           
            #region - Splash -
            App.BoostrapScreen.Show();
            #endregion
        };
    }
}

不要忘记在加载MainWindow.xaml时关闭你的bootstrapScreen。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接