Listbox,WP7中的图像加载问题

Nav*_*rma 5 .net c# silverlight windows-phone-7

我有一个Listbox,其中每个元素都有一个已经存储为内容的图像.我选择使用转换器显示的图像.

如果相应值的图像不存在,我必须显示我在ImageFailed事件中处理的默认图像.

问题是当我运行程序时,我得到了一些已经存在的图像的默认图像.如果我向下滚动列表框并再次备份,有时正确显示的图像将显示默认图像.这似乎是一个性能问题.

我是应用程序开发的新手,让我知道任何细节,即使它对你来说似乎微不足道.

以下是我的实施

<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>
Run Code Online (Sandbox Code Playgroud)

转换功能

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;
}
Run Code Online (Sandbox Code Playgroud)

ImageFailed处理程序

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;
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*cey 0

是否因为您的Convert方法没有使用传入的值而是查找prgSvcId

如果您从 XAP 中加载图像,您可以测试它们是否存在,而不是依赖加载默认/替代图像的失败。

测试文件是否存在:

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