How to preload XAML at app startup?

后端 未结 2 1668
夕颜
夕颜 2021-02-10 10:27

I have pretty large UserControl that\'s not showed at main screen, but user almost always uses it later. It takes some time to load for the first time (parse BAML etc.) then oth

相关标签:
2条回答
  • 2021-02-10 11:22

    You could use the App ctor or Startup

        App()
        {
            System.Diagnostics.Debug.WriteLine("App ctor");
            //ctor
        }
    
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("App Startup");
        }
    
    0 讨论(0)
  • 2021-02-10 11:29

    To preload complex UIs so that they take less time when they are actually "viewed" on the screen, we would have to follow following steps...

    1. Make it load in Invisible mode. Change all you bindings to visibility trigger based. This way UI thread would neither block nor take time to perform the rendering in invisible mode.

      <Style TargetType="TextBlock">
              <Style.Triggers>
                      <Trigger Property="IsVisible" Value="True">
                              <Setter Property="Text" Value="{Binding Value}"/>
                      </Trigger>
              </Style.Triggers>
      </Style>
      
    2. Separate the data context (viewmodel) loading from the UI loading. What this means is any data represented by the user control can be loaded on worker thread and then the UI must be notified using Dispatcher.BeginInvoke(). Make sure that this happens when UI is visible otherwise the bindings would take effect due to step 1.

    3. When the UI is actually "viewed", choreograph the loading of UI's regions ... e.g. Use Expanders and collapse them by default... but when the UI is viewed, start sliding the expander using sliding animation and content opacity animation to show the contents inside it etc...

    IN our application we employed such techniques to achieve complex UIs to load fast and be responsive. One such UI which was a geographical map when viewed would not only block the UI thread but also take 20 seconds to load. Using above steps the loading was reduced to 4 seconds and no blocking ocurred of the UI thread.

    I hope these steps will help you too.

    0 讨论(0)
提交回复
热议问题