wp7 - TextBlock with a lot of text - huge memory usage - how to avoid it?

后端 未结 3 1040
小蘑菇
小蘑菇 2020-12-19 21:42

I have a problem in my app and I don\'t know if it is normal or not. I have a textblock in my application that needs to display a large amount of text (2000-4000 chars). Any

相关标签:
3条回答
  • 2020-12-19 21:59

    I know this is an old question, however I wanted to add one more solution.

    http://blogs.msdn.com/b/stankovski/archive/2013/08/27/yet-another-scrollable-textblock-for-windows-phone.aspx

    To accomplish my task I have encapsulated the "splitting" logic into a separate class that produces the output as a List of strings. You can then bind that list to your favorite ListBox control and voila, you have a ginormous text block. The splitting logic has been optimized for performance so you'll get a much better processing time then ScrollableTextBlock by Alex. Also, since you can bind the list to any ListBox control that supports virtualization you will have a much more conservative memory footprint.

    0 讨论(0)
  • 2020-12-19 22:02

    For a large amount of text in a single control it is normal to consume larger amounts of memory than expected. As it was mentioned before, you can page the text or implement dynamic loading, where only chucks of text are loaded for the visible area. That way you won't be keeping a large string in memory.

    In your case, caching would be related to re-use and content reloads whenever the user switches to a different page, not to the initial content loading and manipulation process.

    0 讨论(0)
  • 2020-12-19 22:09

    I came across similar issues when creating Overflow7

    The problems I encountered were to do with the fact that if you use a StackPanel inside a ScrollViewer, then the ScrollViewer insists that all of the StackPanel is rendered, not just the visible portion.

    I read around and there were 2 general solutions:

    • use UI virtualization techniques - e.g. VirtualizingStackPanel
    • use data virtualization techniques - e.g. crafting your own paging

    To get round this in Overflow7 I made use of ListBoxes instead of the ScrollViewer/StackPanel combo. The internals part of ListBox use a VirtualizingStackPanel - and this VirtualizingStackPanel renders just whats on the screen, not the entire scrollable client area.

    This was a bit "hacky" but worked well. If you have time, then I believe a better solution would be to improve the ScrollableTextBlock implementation so that it uses VirtualizingStackPanel - there are good posts about how to use this on (for example) WPF VirtualizingStackPanel for increased performance

    0 讨论(0)
提交回复
热议问题