开发者

WPF InitializeComponent performance problems

开发者 https://www.devze.com 2023-01-26 09:28 出处:网络
I have a WPF application (.NET 4) which has a main window, and inside that main window shows many smaller UserControls. Various actions performed by the user cause the UserControls which are displayed

I have a WPF application (.NET 4) which has a main window, and inside that main window shows many smaller UserControls. Various actions performed by the user cause the UserControls which are displayed to get replaced by different other controls with different data.

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!

Profiling indicates that almost al开发者_StackOverflow中文版l of this CPU time is spent calling the various InitializeComponent methods of all the different UserControls - 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).

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

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.

Otherwise it looks like the only options I have are to delete all my xaml??

Any help would be greatly appreciated


To revisit this - we do have many complex controls on the screen, but we can't just get rid of them to keep WPF happy!

Further experimentation with profiling showed that using Custom controls (basically just a C# class deriving directly from Control and defining the UI in a Generic.xaml themes file only seem to incur the hit of loading and parsing the XAML once. Thereafter, each control just applies the pre-existing theme.

Custom controls are much more difficult to work with than UserControls, but this did seem to help our load performance a lot.


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

My only suggestion is - is it possible to initialise all potential controls from the outset, and then show/hide them when needed using the Visibility property ?

You can use Freezable for caching some UI, but if they are user controls then most likely you will want your user to interact with them.


For the record, I had a window with a load time about 1500 ~ 2000 ms, the problem was the icons.

I was using a tool for converting SVG to XAML DrawingImage elements, and a user control with a large resource dictionary with a drawing image for every used icon

The InitializeComponent was so slow because it had to parse that large XAML file containing all the vector data for the images

Hope it helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号