使用启动画面改善WPF应用程序感知的冷启动性能
本文关键字:感知 应用程序 冷启动 性能 WPF 启动 动画 | 更新日期: 2023-09-27 18:02:01
我的WPF应用程序启动缓慢(冷启动),我想让应用程序的主窗口出现,只要用户双击应用程序的图标。
我读了这个博客,我想添加一个启动屏幕来避免这种延迟。我在我的应用程序中添加了一个启动屏幕(PNG图像),但我有一个问题:
我如何添加初始化代码来改善启动,或者启动屏幕将显示,直到应用程序加载所有所需的组件
这取决于您在哪里加载资源,这些资源非常耗时,以至于MainWindow
被延迟了很多。如果你已经在XAML的<App.Resouces />
块中创建了这些,那么它就很棘手了。
当它们在<MainWindow.Resources />
中被创建为视图模型的资源时,然后非常简单地创建一个工具窗口或类似的包含启动屏幕的工具窗口,并在Application_Startup
事件中显示它,如:
public partial class App : Application
{
// A Splash-Window to overlay until everything is ready.
public SplashWindow AppLauncher = new SplashWindow();
private void Application_Startup(object sender, StartupEventArgs e)
{
AppLauncher.lblText.Content = "Loading data...";
AppLauncher.Show();
}
}
当所有的资源都在MainWindow
中加载时,然后在Loaded
事件中再次隐藏/处置窗口。
public partial class MainWindow : Window
{
private void Window_Loaded(object sender, RoutedEventArgs e)
{
(App.Current as App).AppLauncher.Close();
}
}