空白 - 如果图像源是绝对 Uri,则 WPF 中的黑色图像控件

发布于 2024-10-15 13:53:38 字数 515 浏览 3 评论 0原文

我在列表框控件上使用自己的数据模板。列表框项目由一个图像控件和一些文本块组成。

在图像源上,我绑定 Uri 的属性类型(绝对 url - 例如: http://u.aimg.sk/fotky/1730/71/17307141.jpg?v=2)

列表框大约有 50 - 300 个项目。

如果我测试应用程序,我有时会看到空白 - 白色或黑色图像而不是用户图像。

您可以在此图像上看到的问题:

在此处输入图像描述 在此处输入图像描述

我想知道导致此问题的原因以及如何解决此问题。 图片来源很好,我在浏览器中查看。

感谢您的建议。

I use on listbox control own datatemplate. Listbox item consist one image control and some textblock.

On image source I bind property type of Uri (absolute url - for example: http://u.aimg.sk/fotky/1730/71/17307141.jpg?v=2)

Listbox have about 50 - 300 items.

If I test app, I sometimes see blank - white or black image instead user images.

The problem you can see on this images:

enter image description here
enter image description here

I would like to know what cause this problem and how can I solve this problem.
Image sources are good, I check it in browser.

Thank for advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝色星空 2024-10-22 13:53:38

我认为正在发生的事情是一种竞争条件。当您要求显示某些图像时,某些图像尚未完成下载。这里给出了一个很好的例子 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();

这是因为默认情况下使用源的图像将延迟加载它,然后您的流可能已关闭,这也可能是您获得空白/黑色图像的原因。

祝你好运。

I think what's happening is a race condition. Some of your images haven't completed downloading by the time you are asking them to display. There is a pretty good example given here http://social.msdn.microsoft.com/Forums/en/wpf/thread/dc4d6aa9-299f-4ee8-8cd4-27a21ccfc4d0 which I'll sum up:

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;
}

With the above code, your images that are binding to your property will be told to update with the PropertyChanged call when the value is first assigned as well as AFTER the images have downloaded 100%. This is taken care of in the DownloadCompleted event handler that is utilized in the above example. This should make them not appear as a black image anymore, but as their fully-ready selves.

Also, if you are using a stream to as the source for your images, you need to make sure you use BitmapCacheOption.OnLoad. Such as:

BitmapImage source = new BitmapImage();
source.BeginInit();
source.CacheOption = BitmapCacheOption.OnLoad;
source.StreamSource = yourStream;
source.EndInit();

This is because by default the image using the source will lazy load it and by then your stream is probably closed, which could also be why you get blank/black images.

Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文