显示初始屏幕后延迟导致问题
本文关键字:问题 延迟 屏幕 显示 | 更新日期: 2023-09-27 18:37:22
>我有一个初始屏幕,我想淡入,而我的应用程序关闭并执行某些操作,然后在完成后,如果经过一定的时间,关闭初始屏幕并加载主屏幕。
我得到的问题是,当我循环检查时间是否经过时,它似乎没有处理我的故事板以淡入初始屏幕。
阅读后,我想我应该在不同的线程上启动启动画面吗?
这是代码:
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
MySplashScreen splash = new MySplashScreen();
splash.Show();
Stopwatch sw = new Stopwatch();
sw.Start();
while (sw.Elapsed.TotalSeconds < 10)
{
}
splash.Close();
MainWindow mw = new MainWindow();
mw.Show();
}
}
问题是当我添加动画时。就像它没有将任何处理器应用于淡入淡出,因为它卡在了 while 循环中。
<Window x:Class="Splash_Demo.MySplashScreen"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="550" Width="900" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None" ShowInTaskbar="False" Background="Transparent" AllowsTransparency="True" Opacity="0">
<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="00:00:03" Storyboard.TargetProperty="Opacity" To="1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
<Canvas Height="498" Width="839">
<Canvas.Background>
<ImageBrush ImageSource="C:'Users'Ash'Downloads'XactSplash.png"/>
</Canvas.Background>
<Label Canvas.Left="291" FontFamily="Algerian" Canvas.Top="413" Name="Customer" Height="43" Width="185" HorizontalContentAlignment="Left" VerticalContentAlignment="Center"/>
<Image Canvas.Left="500" Canvas.Top="165" Height="164" Name="image1" Stretch="Fill" Width="211" Source="C:'Users'Ash'Downloads'Zerix.bmp" />
<Label Canvas.Left="191" Canvas.Top="376" FontSize="8" Content="Label" Height="19" Name="lblYear" Width="30" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" />
</Canvas>
</Window>
您正忙于等待,导致 UI 线程阻塞,并在此过程中占用大量 CPU。相反,请在Application_Startup中设置一个计时器,并在它到期时打开主窗口。
在"while"循环中,您无法运行任何动画,因为您的 UI 线程被阻止(因此等待运行动画)。尝试改用DispatcherTimer
使用 API 的最佳方法是
SplashScreen splash = new SplashScreen("splashscreen.jpg");
splash.Show(false);
splash.Close(TimeSpan.FromMilliseconds(2));
InitializeComponent();