user1482528
user1482528

Reputation: 53

Paging data / infinite scrolling on Windows Phone

I want to know is there any other solution of paging data on WindowsPhone Listbox control. I am used to paging data, by manually checking scrollbar position, and when it reaches the end of screen, then more data is loaded.

I want to ask, is there any other solution (better) of doing this (WP7 or WP8). My solution, which I've mentioned is connected with writing a lot of code (custom events, scroll listener, etc.) I think, there might be an easier solution, as WindowsPhone sdk 8.0 has been released...

Upvotes: 1

Views: 4893

Answers (2)

gleb.kudr
gleb.kudr

Reputation: 1508

The key to WP8 infinite scrolling is LongListSelector control and handling the two events:

ItemRealized (loads item) and ItemUnrealized (removes item).

This events are raised automatically based on the detected static template size(height). It is a completely automatic, on-demand action. The engine "knows" when it should get the new item or remove the old one.

Essential:

  1. You must set the template size manually. Otherwise the count of simultaneously loaded items is unpredictable. For example if you have Image in your template it does not count as space, unless it has specific height.
  2. ContentPresenters of List items seems to recycling. So if you change one item template from code you could magically get the new template for another item to! ;) So... Remember your ItemTemplates and set it on every ItemRealized event if you need two or more.
  3. Unloading of BitmapImage data is pretty buggy. If you have Image in your item template, you should manually free it cache on ItemUnrealized event. And also destroy and reset binding by setting Image.Source to null. And set the original source BitmapImage to new small-sized source with minimal decodepixelwidth/height (you can't null it). I found it is the only way to fight the memory leaks. Here is the details https://stackoverflow.com/a/14225871/1449841
  4. Binded collection for data virtualization could be as big as you want (million is ok). So you should bind that collection once and your only business will be loading and unloading items data by mentioned events.

Upvotes: 2

Rudi Visser
Rudi Visser

Reputation: 21969

You can use the VisualStatesGroups that are HorizontalCompression and VerticalCompression, so that you're able to detect when you're around the bottom of a ListBox.

This has been around since Mango so isn't a new WP8 feature, but definitely sounds better than your current solution of "hacky" event handlers.

Read about it on MSDN Blog - "Windows Phone Mango change, Listbox: How to detect compression(end of scroll) states ?"

If you're not avert to changing the control, there is LongListSelector, found in the Windows Phone Toolkit (note this is native in WP8) which may certainly be a better way to approach the problem, it's mentioned in the linked article that this is the new way to do what the old article got at for Mango.

Upvotes: 0

Related Questions