【问题标题】:Improve WPF Application Perceived Cold Startup Performance Using Splash Screen使用启动画面提高 WPF 应用程序感知的冷启动性能
【发布时间】:2016-08-11 12:18:39
【问题描述】:

我的 WPF 应用程序启动很慢(冷启动),我想让应用程序主窗口在用户双击应用程序图标时立即出现。

我阅读了这个Blog,我想添加一个启动画面以避免这种延迟。我在我的应用程序中添加了一个启动画面(PNG 图像)但我有一个问题:

如何添加初始化代码以改善启动,或者启动画面会一直显示,直到应用程序加载所有必需的组件?

在此致谢

【问题讨论】:

    标签: c# wpf startup splash-screen


    【解决方案1】:

    这取决于您加载资源的位置,这些资源非常耗时,以至于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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-22
      • 2020-09-20
      • 2011-07-22
      • 2017-06-26
      • 2022-01-22
      相关资源
      最近更新 更多