title: WPF InitializeComponent performance problems
body: <p>I have a WPF application (.NET 4) which has a main window, and inside that main window shows many smaller <code>UserControls</code>. Various actions performed by the user cause the <code>UserControls</code> which are displayed to get replaced by different other controls with different data.</p>
  
  <p>I am running into performance problems however, when switching these controls. The WPF dispatcher thread goes to 100% CPU while loading controls. On older machines, or with larger numbers of controls, this can result in the application appearing to lock up for as long as 30 seconds!</p>
  
  <p>Profiling indicates that almost all of this CPU time is spent calling the various <code>InitializeComponent</code> methods of all the different <code>UserControls</code> - no one control appears to be vastly worse than any other, they all seem to take between 0.2 and 0.5 seconds (on my dev machine with a fast processor and good graphics card).</p>
  
  <p>As far as I know, <code>InitializeComponent</code> is where WPF actually loads the compiled xaml into memory.</p>
  
  <p>I'm at a loss for what to do here. I'd like to pre-initialize things on a background thread, but all WPF controls must be created and used on the dispatcher thread, so I don't think this is possible.</p>
  
  <p>Otherwise it looks like the only options I have are to delete all my xaml??</p>
  
  <p>Any help would be greatly appreciated</p>