Search code examples
c#imagexamluwpvirtualization

FrameworkElement.Loaded method for an image in a virtualized listview


In my ListView I have an Image in my DataTemplate which uses a Converter to load the Image. If the image being loaded dose not exist, the converter loads a placeholder image. Next, in the Image's FrameworkElement.Loaded method I create the missing image and it shows on screen asynchronously. It all works great... except for one thing.

The Loaded Method does not seem to get called for every missing image - that I can tell. And sometimes it will load with null value for the DataContext - but only sometimes? Sometimes The image for image1 is loaded, for example, for image 33.

What usually happens is, for most of the missing images on screen, they will be created and load correctly, but then it just stops near the end of the images on screen. I believe this is due to the way virtualisation works in uwp, as this same method in a wpf app, with virtualisation turned off works as expected.

So my question is:

How do I get access to each image when loaded in the DataTemplate of the ListView? If Loaded is the wrong method, what would the right one be?


Solution

  • The answer is to use the ImageOpened method as I open a placeholder image if the actual one does not exist. So I check in the ImageOpened method that the opened image is the placeholder and call my CreateImage method from there.