I'm loading a lot of images in a listbox. There was a sensible loading time before i tried to virtualize.
Now that i use virtualization only the elements that fall into view are loaded, exactly as i wanted; but they don't seem to unload once they fall outside the view so if i scroll all the way down to the bottom all the items are loaded and i lose the advantage of virtualizing.
Before i display any image, my app consumes 140MB, then i display 10 images and my memory usage equals 161MB, if i scroll to the last image the usage is 300MB. If 10 images use 21 MB, then with 300MB all the images must be loaded.
This behavior is furthermore confirmed by inspecting the number of loaded ListBoxItems with Snoop.
Nothing special in the code, i just make sure CanContentScroll
is true to enable not disable virtualization. I customize the ItemContainerStyle
and the template of the ListBox
but it shouldn't affect the virtualization (or atleast not only the unloading part), isn't it?
<ListBox ItemsSource="{Binding myItems}" CanContentScroll="True"/>
Am I missing something?
CONCLUSION
The virtualization obviously works. Each ItemContainer is unloaded at some point when they fall outside the view.
The problem is probably how the BitmapImage caches. In facts, i'm using the following DataTemplate to show the images:
<Image>
<Image.Source>
<BitmapImage UriSource="{Binding}" />
</Image.Source>
</Image>