.NET FW中的BitmapImage类何时下载/缓存?

4

我对这个类有点困惑,希望有人可以解释一下。 我知道它何时下载取决于图片的BitmapCreateOptions

然而,当你创建一个绝对BitmapImage时:

var Image = new BitmapImage(new Uri("http://...", UriKind.Absolute))

我不会立即下载它,因为延迟创建是默认的BitmapCreateOptions,对吗?

如果你这样做会怎样:

var Image = new BitmapImage(new Uri("http://...", UriKind.Absolute))
Image.CreateOptions = BitmapCreateOptions.None;

在设置其BitmapCreateOptions后,它会立即开始下载图像吗?如果是这样,那么它的行为与此相同,对吗?

var Image = new BitmapImage(new Uri("http://...", UriKind.Absolute)) { CreateOptions = BitmapCreateOptions.None }

好的,现在,BitmapImage 的缓存是如何工作的?

  1. 什么时候会对 BitmapImage 进行“缓存”?
  2. 只有下载的例如“绝对”图像会被缓存,还是本地的例如“相对”图像也会被缓存?
  3. 缓存何时/多久刷新一次?
  4. 这是否意味着我不需要担心在我的 Windows Phone 项目中手动缓存图像到隔离存储中?

最后,ImageOpenedImageFailed 事件在何时触发?

  1. BitmapImage 被下载时,它们会被触发吗?
  2. 或者当 BitmapImage 从缓存中加载时,它们会被触发吗?
  3. 还是当它们在屏幕上呈现时才会被触发?

谁知道图片缓存是怎么工作的?哈哈 - Francisco Aguilera
1个回答

1
我知道这已经晚了几个月,但是要记录一下,下载发生在调用EndInit时,此后对属性的任何其他更改都将被丢弃。使用除默认构造函数以外的构造函数将自动初始化图像。
换句话说:
var Image = new BitmapImage(new Uri("http://...", UriKind.Absolute));
// The image is now intialized and is downloading/downloaded
Image.CreateOptions = BitmapCreateOptions.None; // nothing happens here

如果您想手动初始化属性,可以这样做:
var Image = new BitmapImage();

Image.BeginInit();
Image.UriSource = new Uri("http://...", UriKind.Absolute)
Image.CreateOptions = BitmapCreateOptions.None; // This is default anyway so it won't affect
// ..Setting other properties...
Image.EndInit();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接