c# - 列表框中的图像加载问题,WP7

标签 c# .net silverlight windows-phone-7

我有一个列表框,其中每个元素都有一个已作为内容存储的图像。 我使用转换器选择要显示的图像。

如果相应值的图像不存在,我必须显示默认图像 我已在 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/

相关文章:

c# - 如何使用 C# 访问 jar 文件(其中包含 Java .class 作为 API)

c# - Windows Phone 7 中的 XDocument.Parse 有何不同?

c# - 使用在 Threadpool 线程上创建的 BitmapImage

c# - Unity 使玩家保持在屏幕中央

c# - 将证书导出为 BASE-64 编码的 .cer

c# - 指定 Xaml 中开始和结束标记之间的属性

silverlight - 在 Silverlight 4 中托管 Microsoft Office 应用程序?

c# - 设置 Windows Phone 8.1 WebView 的基本 URL 并使用 NavigateToString()

c# - 编程通用 Remote

.net - 如何判断候选人是否是优秀的 Sharepoint 架构师/开发人员?