Naveen Kumar Sharma
Naveen Kumar Sharma

Reputation: 111

Image Loading problem in Listbox, WP7

I have a Listbox in which every element has an image that is already stored as content. I choose the image to be displayed using a converter.

If the image for the corresponding value does not exist, I have to display a default image which I have handled in the ImageFailed event.

The problem is that when I run the program I am getting the default image for a few images that already exist. If I scroll down the list box and back up again sometimes an image which was displayed properly displays the default image. This seems to be an performance issue.

I am new to application development, Let me know any detail even though it might seem trivial to you.

Below is my implementation

<ListBox DataContext="{Binding}">
    <ListBox.ItemTemplate>
        <DataTemplate>
                <StackPanel>
                            <Image Width="90" Height="67" Source="{Binding id,Converter={StaticResource imageConverter}}" ImageFailed="ImageFailed" />
                             _
                             _
                    </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

The convert function

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
       string Id = (string)value;
   string imagePath;
   imagePath = string.Format(AppDefines.channelLogoImgPath, prgSvcId);
   return imagePath;
}

The ImageFailed handler

private void ImageFailed(object sender, ExceptionRoutedEventArgs e)
{
    Image Img = (Image)sender;
    string imgPath = Defines.defImagePath
    Uri uri = new Uri(imgPath, UriKind.RelativeOrAbsolute);
    BitmapImage bDefImage = new BitmapImage(uri);
    Img.Source = bDefImage;
}

Upvotes: 5

Views: 1382

Answers (4)

Geoff Webber-Cross
Geoff Webber-Cross

Reputation: 52

I am seeing the same issue, I'm binding a url, then using a service and thread pool to get the image in the background and load it back in. I created a custom control in a list, but the dependency property in the control seems to be returning random urls from all over the list. At first i thought it was some kind of synchronisation issue, but I think it's because the ListBox is not like a normal SL listbox because it uses a VirtualizingStackPanel. There are some guidelines here:

http://blogs.msdn.com/b/slmperf/archive/2010/10/06/silverlight-for-windows-phone-7-listbox-scroll-performance.aspx

Basically don't try and do anything unusual to the list!

Upvotes: 0

Naveen Kumar Sharma
Naveen Kumar Sharma

Reputation: 111

I have got a work around solution to the problem, I put 2 images in the same location. the default image is set visible right from the start. The source for the item specific image is obtained as above (binding). In the image opened event handler of the item specific image the default image's visiblity is set to collapsed.

After doing this, the app is working fine on both emulator and the device. For now there are only 2 things that I can blame.

the Image failed event. there are some cases where the image failed event is triggered and the image is displayed as well. (Probably there is some time limit etc which maked the event to be fired)

Listboxes and images dont go together.

Upvotes: 1

Matt Lacey
Matt Lacey

Reputation: 65564

Is it because your Convert method isn't using the value passed in but is looking up prgSvcId?

If you're loading images from within the XAP you could test for their existence rather than relying on the failure to load the default/alternative image.

To test for a files existence:

if (Application.GetResourceStream(new Uri("/images/myPic.png", UriKind.Relative)) != null)
{
    // file exists
}

Upvotes: 0

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93551

The problem is that your convertor is returning a string (path to the image) and not an ImageSource.

You need something like this instead:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
   string imagePath = string.Format(AppDefines.channelLogoImgPath, value);
   return new BitmapImage(imagePath); 
} 

As Matt noted, you are also not using the id you are passing to the convertor. Simplified code above includes that fix.

Upvotes: 1

Related Questions