gpt4 book ai didi

c# - 带有动画 GIF 的启动画面在 WPF C# 中没有动画

转载 作者:行者123 更新时间:2023-11-30 17:29:49 26 4
gpt4 key购买 nike

情况是这样的。我目前正在开发 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
}
}

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

最佳答案

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

关于c# - 带有动画 GIF 的启动画面在 WPF C# 中没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50664221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com