I have to buil a rather complex WPF UI (a hughe Grid) in code (no xaml invloved). Is there a way to stop the main UI thread from blocking while beeing in the process of building the Grid? Are there some portions of building the UI that can be outsourced to a workerthread? What parts of UI creation actually have to be on the UI thread?
- Calling the constructor of the
Controls? - Setting properties of the controls?
- Setting up Databinding?
- Building the logical tree (adding children)?
What other options do I have to increase performance while building the UI? Is suspending the dispatcher or calling FrameworkElement.BeginInit a good idea?
If you can break the construction of your UI up into steps, you can do each of these steps as a separate message. This will allow other messages to be processed in between. Something like:
You can refactor this to be cleaner (eg. one method with a state machine that tells you where in the grid you are up to), and automatically queue the next message at a lower priority so that input is always processed first. Bingo, you will have a responsive UI whilst building UI on the UI thread.