I'm displaying a collection of images in a LongListSelector on WP8 and I've implemented the well known lazy loading pattern utilizing LLS's ItemRealized event.
In the code below OnItemRealized is called for each and every item in the Pictures collection - even for items which are clearly off-screen. In this scenario 24 items fit on screen yet the LLS realizes 40 items and this triggers the ResumeGetPictures() of the ViewModel. When the Pictures collection changes (INotifyCollectionChanged), the LLS will also realize those items until it runs out of items, triggering the next ResumeGetPictures() - this will go until the ViewModel is unable to load more items.
All seems to be fine as long as the LLS is in LayoutMode=List. But when I switch to Grid, the control seems to swallow each and every item in the list and realizes it immediately. Making any sort of lazy loading impossible.
I hope I just did something very very wrong - although I doubt that because I've triple-checked everything and like I said switching to "List" resolves the problem immediately - unfortunately not an option for a photo gallery of some sort.
ViewModel:
public IReactiveDerivedList<TPicture> Pictures
{
get { return pictures; }
}
View Code-Behind:
lls.ItemRealized += OnItemRealized;
private void OnItemRealized(object sender, ItemRealizationEventArgs e)
{
var picture = e.Container.Content as Picture;
if (picture != null)
{
// get index
var pictureIndex = lls.ItemsSource.IndexOf(picture);
if (pictureIndex >= lls.ItemsSource.Count * 0.95f)
ViewModel.ResumeGetPictures();
}
}
XAML:
<phone:LongListSelector Name="lls" Margin="13,-30,0,0"
ItemsSource="{Binding Pictures}"
Tap="OnListItemTapped"
ItemTemplate="{StaticResource ItemTemplate}"
IsGroupingEnabled="False"
LayoutMode="Grid"
GridCellSize="108,108"/>
Count
is a integer. You might as well been multiplying with one. As for your question, are you saying thatOnItemRealized
gets called 40 times, if you remove all your logic inside it? – Claus Jørgensen