我有一个列表框,其中每个元素都有一个已作为内容存储的图像。 我使用转换器选择要显示的图像。
如果相应值的图像不存在,我必须显示默认图像 我已在 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>
转换函数
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;
}
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;
}
最佳答案
问题是您的转换器返回的是字符串(图像路径)而不是 ImageSource。
你需要这样的东西:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string imagePath = string.Format(AppDefines.channelLogoImgPath, value);
return new BitmapImage(imagePath);
}
正如 Matt 指出的那样,您也没有使用传递给转换器的 ID。上面的简化代码包括该修复。
关于c# - 列表框中的图像加载问题,WP7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6995881/