【问题标题】:Splash Screen with Animated GIF is not animating in WPF C#带有动画 GIF 的启动画面在 WPF C# 中没有动画效果
【发布时间】:2018-11-12 19:45:35
【问题描述】:

所以情况就是这样。我目前正在开发一个 C# WPF 桌面应用程序,我想在启动程序时添加一个启动画面。

但我没有使用 SplashScreen 类。我在同名应用程序之上使用了一个额外的 WPF Windows 类,我将该类称为 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();
    }
}

下面是实现splashScreen的程序方法。

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

    public LoginScreen()
    {

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

        // home screen 
    }
}

任何指点和帮助将不胜感激。

【问题讨论】:

  • 很可能是因为您正在休眠 UI 线程?
  • 程序员通常使用闪屏来掩盖他们的 UI 线程没有响应的事实。忙于初始化自身。 “不响应”包括不获取 GIF 动画。不是您应该考虑修复的问题,因为这很难正确执行,并且您可能不再需要启动屏幕了:) 保持简单。
  • @Ross Chris 有空请标记为正确。

标签: c# wpf splash-screen


【解决方案1】:

我会尝试这样的事情。创建一个处理所有这些逻辑的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);
    }
}

【讨论】:

  • 非常感谢你,拉特!很抱歉回复晚了,但我真的很感谢你在这里的帮助。这种方法完全节省了我的时间和生命。 :)
【解决方案2】:

您好,如果您想在 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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    • 2013-11-26
    • 1970-01-01
    • 2022-11-11
    • 2014-04-04
    • 2014-01-09
    相关资源
    最近更新 更多