Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 567
Naveen Kumar Sharma Avatar asked Nov 04 '22 16:11

Naveen Kumar Sharma


1 Answers

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.

like image 163
Gone Coding Avatar answered Nov 14 '22 21:11

Gone Coding