Problema de rendimiento al cargar muchos controles de usuario

My application is loading a bunch of the same user control into a ScrollPanel. The problem is, this is very slow.

The profiler show that the method Application.LoadComponent(), which is called internally by in the designer code in the constructor of my user control, is the bottleneck. The documentation of this method says, that this method load XAML archivos.

The question is, how can I use BAML instead of XAML? How can I accomplish that the XAML for my user control must not be parse again and again when creating new instances from it? Is there another way to make loading my user controls faster?

preguntado el 08 de enero de 11 a las 20:01

1 Respuestas

LoadComponent() already loads .baml, don't worry about this bit of the question. Microsoft did this intentionally, to not let developers make dependencies on baml format. Going forward they can improve the format without breaking any existing applications.

Yes, there are another ways of making it work faster. The first optimization to take is UI virtualization. WPF already comes with handy VirtualizandoStackPanel. It works in tandem with ItemsControls, and has certain limitations (e.g. if you create items containers and add them by your own, you loose virtualization, or if you set ScrollViewer.CanContentScroll="False" you loose it again). To use virtualization you will probably have to rewrite your application to use ItemsControl + DataBinding estilo (ListBox already has virtualization enabled)

If you feel like you need even more information on UI virtualization refer to Dan Crevier's blog.

And final advice. You can try rewriting your UserControls to Custom Controls. My simple performance test showed the following figures. To create 10K controls with the same visual tree it took:

  • for UserControl: 4932ms;
  • for CustomControl: 86 ms; (~57 times faster)

Espero que esto ayude

Respondido el 09 de enero de 11 a las 01:01

The MSDN page (…) states explicitly that LoadComponent loads XAML, not BAML, that's the problem. Since my UserControl is a composition of multiple other controls, a custom control won't be the right choice here. - codymanix

You want to use UI Virtualization as suggested, which will only display the items on screen. Using BAML won't improve your performance. BAML is just a compiled version of XAML. - Kevindaub

Can you expand upon "You can try rewriting your UserControls to Custom Controls"? I understand the difference so far as inheritance vs. composition, but I don't understand why there is such a dramatic performance difference. - Trinición

I think you could also do the composition along with the inheritance by writing Custom Control. The performance difference might be caused by the different approaches of building visual and logical tree based on xmal/baml. The UserControl itself is a ContentControl while the Custom Controls(in most cases) just subclass of Control. It will need to handle the ContentTemplate in the case of UserControl. P.S. I never did this kind of test. - terry

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.