我在列表框控件上使用自己的数据模板。列表框项目由一个图像控件和一些文本块组成。
在图像源上,我绑定了 Uri 的属性类型(绝对 url - 例如:http ://u.aimg.sk/fotky/1730/71/17307141.jpg?v=2 )
列表框有大约 50 - 300 个项目。
如果我测试应用程序,我有时会看到空白 - 白色或黑色图像而不是用户图像。
您可以在此图像上看到的问题:

我想知道是什么导致了这个问题,我该如何解决这个问题。图片来源很好,我在浏览器中查看。
感谢您的建议。
我认为正在发生的事情是一种竞争条件。在您要求显示时,您的某些图像尚未完成下载。这里给出了一个很好的例子http://social.msdn.microsoft.com/Forums/en/wpf/thread/dc4d6aa9-299f-4ee8-8cd4-27a21ccfc4d0我总结一下:
private ImageSource _Src;
public ImageSource Src
{
get { return _Src; }
set
{
_Src = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Src"));
((BitmapImage)_Src).DownloadCompleted += new EventHandler(MainWindow_DownloadCompleted);
}
}
void MainWindow_DownloadCompleted(object sender, EventArgs e)
{
PropertyChanged(this, new PropertyChangedEventArgs("Src"));
((BitmapImage)_Src).DownloadCompleted -= MainWindow_DownloadCompleted;
}
使用上面的代码,绑定到属性的图像将被告知在首次分配值时以及在图像下载 100% 之后使用 PropertyChanged 调用进行更新。这在上面示例中使用的 DownloadCompleted 事件处理程序中得到了处理。这应该使他们不再显示为黑色图像,而是完全准备好自己。
此外,如果您使用流作为图像的来源,则需要确保使用 BitmapCacheOption.OnLoad。如:
BitmapImage source = new BitmapImage();
source.BeginInit();
source.CacheOption = BitmapCacheOption.OnLoad;
source.StreamSource = yourStream;
source.EndInit();
这是因为默认情况下,使用源的图像将延迟加载它,届时您的流可能已关闭,这也可能是您获得空白/黑色图像的原因。
祝你好运。