开发者

Tweak WPF application performance which hosts hundreds of similar controls

We just ported our WinForms application to WPF. However, performance decreased dramatically.

We have a User Interface which consists of about 200 UserControl. Each UserControl is defined by a 开发者_如何学CDataGrid (= 10 columns and 3-15 rows) as well as a Panel which hosts about 10 Buttons.

They are all hosted in a ScrollViewer.

(Please don't recommend to change the UI. I don't have any influence on that. The customer wants to be able to scroll to any of those UserControls.)

Since we ported the whole application to WPF the startup time increased by 100%. Using WinForms we experienced startup times of 15sec whereas now, we are struggeling with 30s.

Do you have any recommandations or ideas how to improve the loading time of a UI which consists of identical UserControl where simply each UserControl is bound to a different ViewModel? (Maybe some fast cloning of the UserControl instances or sth similar?)

I am using static Resources whereever possible. I avoid Grids and Auto Sizing whereever possible.

Hope someone can share some thoughts on that one.

Thanks, TH


First find out what is responsible for the time.

Maybe it's the controls, and maybe not. Often it's data structure.

I use the random-pause method.


My final solution is a custom Virtual Panel which supports items of dynamic height.

See http://rhnatiuk.wordpress.com/2006/12/13/implementing-a-virtualized-panel-in-wpf/ on how to create virtual panels.

My UserControls support two states: - Initial - Loaded

When the application is in idle the virtual Panel asks the Controls to change to the "Loaded" state. This loads the expensive UserControl.

Like that everything is lazy loaded and as soon as the user stops scrolling the visible items are loaded.

Maybe that helps others that are in the same sitaution. TH


Try only to create the controls which are visible at the time, use lazy loading.

Maybe SnapsToDevicePixels=true can also help a little bit.


Guys, I thought about the following implementation. If anyone has concerns please let me know:

I will implement my own virtualizing "StackPanel" which supports smooth scrolling.

For the moment we assume that the height of my UserControls is fixed. One page could possibly hold 5 UserControls.

I will then go ahead and cache the preceding as well as the proceeding page:

In memory I will always hold 15 UserControls. The content of the ScrollViewer is a Canvas. Locations of my UserControls are adjusted by setting Canvas.Top. Let's say the current situation is the following:

User has scrolled to page 2. That means UserControl 5-9 is visible. Now the user scrolls down. As soon as UserControl 5 becomes invisible I take the UC of the top (in this case UserControl 0), change its ViewModel and adjust its Canvas.Top so that it now is the Control which is at the End of the ControlCollection. If the user scrolls any further I take UC 1, change its ViewModel and adjust its Canvas.Top. And so on.

Furthermore I will set Canvas.Height manually so that the ScrollViewer represents the ScrollBars in a correct way.

I hope my explanation is understandable :)

What do you think?

BR, TH


I remember reading something about how each instance of a UserControl loads the resource dictionary. So if you have as many of these as you describe it can take a while to load those.

Unfortunately I can't find the link I remember, but here is one that might help: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/c9b6aa9f-5a97-428c-8009-5cda432b820c

Another thing to try is to not use UserControls and instead use a DataTemplate to build your datagrids and buttons. Or make a custom control template for the datagrid that includes the buttons. Either one might be faster.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜