WP7 LongListSelector– Be aware of its problems
I spent the last weekend working with a small Windows Phone 7 news reader app. First I used the default ListBox control to layout the news items but when I later on decided that I would like to group the items nicely, I replaced the ListBox with the LongListSelector from the Silverlight Toolkit.The control brought me the ability to group the items, and it brought me tons on problems and peculiar behavior.
There’s couple big problems with the control which forced me to write stupid hacks in the app:
Problem: When you navigate back to a page which contains LongListSelector, the list may stay empty until you scroll it.
Solution: Call the ScrollTo-method of the LongListSelector when you navigate back to the page. ScrollTo forces the control to redraw itself. ScrollTo requires an object (not index) so you may need get the first item from your item collection and use that as the parameter. You can also store the current selected item from the list (SelectedItem) to somewhere safe when navigating away. When you navigate back to the page, call the LongListSelector’s ScrollTo-method to move back to the correct point.
Problem: The LongListSelector may throw NullReferenceException when you navigate back to the page.
Solution:If your LongListSelector is bound to a collection and you call NotifyOfPropertyChange for the collection when you navigate back to the page, the control may throw NullReferenceException (in its balance-method). The solution is to avoid the call to NotifyOfPropertyChange. There’s also some more tips available from the CodePlex-forums.
Overall, I recommend anyone to think twice before using the LongListSelector. The grouping-functionality is nice but in my humble opinion it’s not worth the problems the control causes.