Why is Wpf's DrawingContext.DrawText so expensive?

后端 未结 3 1036

In Wpf (4.0) my listbox (using a VirtualizingStackPanel) contains 500 items. Each item is of a custom Type

class Page : FrameworkElement
...
protected over         


        
3条回答
  •  遥遥无期
    2020-12-31 14:30

    While this isn't entirely useful to you, my experience with VirtualizingStackPanel isn't that it disposes of objects not in view, but that it allows objects not in view to be disposed to recover memory when the application needs more memory, which should result in your memory usage ballooning when there is memory available.

    Is it possible that dc.DrawText is firing BuildGeometry() for each formattedText object, and that you can bring that outside the loop? I don't know how much work BuildGeometry is, but it's possible that the DrawingContext is only capable of accepting geometry, and the BuildGeometry call is being called unnecessarily 999 times in your sample. Have a look at:

    http://msdn.microsoft.com/en-us/library/system.windows.media.formattedtext.aspx

    to see whether there are any other optimizations you can make.

    Can you output some memory profile data and some timing data within your loops to give a sense of whether it's slowing down, or the memory is increasing in a non-linear fashion during the loop?

提交回复
热议问题