WPF InitializeComponent Performance Issues

I have a WPF application (.NET 4) that has a main window, and much less UserControls displayed inside this main window. Various actions performed by the user cause the displayed UserControls be replaced by other other controls with different data.

When I switch these controls, I run into performance issues. The WPF dispatch thread switches to 100% processor when loading controls. On older machines or with a large number of controls, this can lead to the application being blocked for up to 30 seconds!

Profiling shows that almost all this CPU time is spent on calling various InitializeComponent methods of all different UserControls - not a single control is significantly worse than any other, they all seem to take from 0.2 to 0.5 seconds (on my a car with a fast processor and a good graphics card).

As far as I know, InitializeComponent is where WPF actually loads the compiled xaml into memory.

I don’t understand what to do here. I would like to pre-initialize things in the background thread, but all WPF controls should be created and used in the dispatcher thread, so I don't think this is possible.

Otherwise, it looks like the only parameters I have to delete all my xaml ??

Any help would be greatly appreciated.

+7
performance c # wpf
source share
3 answers

To revise this, we have many complex controls on the screen, but we cannot just get rid of them so that WPF is happy!

Further experimentation with profiling showed that the use of user controls (basically, only the C # class, obtained directly from Control and defining the user interface in the Generic.xaml theme Generic.xaml , only seems to take a hit at loading and analyzing XAML once., each control simply applies a pre-existing theme.

User controls are much more difficult to work with UserControls, but this seems to have helped a lot with our workload.

+2
source share

The InitializeComponent method takes time because it needs to insert a control into the Visual / Logical tree and provide all the bindings, themes, expected resources, etc.

My only suggestion is is it possible to initialize all potential controls from the very beginning, and then show / hide them if necessary using the Visibility property?

You can use Freezable to cache some user interface, but if these are user controls, most likely you will want your user to interact with them.

0
source share

For recording, I had a window with a boot time of about 1500 ~ 2000 ms, the problem was in the signs .

I used a tool to convert SVG to XAML DrawingImage elements and a user control with a large resource dictionary with a picture for each icon used

InitializeComponent was so slow that he had to parse this large XAML file containing all the vector data for the images

Hope this helps.

0
source share

All Articles