图像源和缓存 [英] Image Source and Caching

查看:31
本文介绍了图像源和缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码显示来自网络服务器的图像:

 

图像会自动下载,我假设还有一些基于 Url 的缓存.

我的问题是,当应用离线时,可能缓存的图像不会显示.

有什么办法可以改变缓存行为,以便在没有可用网络的情况下也加载图像?指向有关缓存的文档的指针也会非常有帮助.

解决方案

BitmapImage 默认自动缓存远程图像.它最好与 CreateOptions="BackgroundCreation" 结合使用以获得最佳性能.

<图片来源><BitmapImage UriSource="{Binding ImgURL}" CreateOptions="BackgroundCreation"/></Image.Source></图片>

这篇 MSDN 博客文章虽然陈旧但仍然相关,它列出并解释了所有 CreationOptions 并且在大多数模式下缓存是自动的.>

我使用这些选项来显示许多带有图像的新闻,而且效果很好.我可以加载文章列表,退出应用程序并将飞行模式打开,然后启动应用程序的新实例,图像仍然加载.

手动方法

如果您想自己控制缓存并缓存 HTTPS 资源,那么有几个很好的例子...

I use the following code to show images from a webserver:

   <Image Source="{Binding Url}" />

The image gets automatically downloaded, and I assume there is also some caching based on the Url.

My problem is, that when the app is offline, the assumably cached images are not shown.

Is there any way to change the caching behavior, so that images are also loaded when there is no network available? Pointers to documentation regarding the caching would be very helpful as well.

解决方案

BitmapImage automatically caches remote images by default. It's best used in conjunction with CreateOptions="BackgroundCreation" for the best performance.

<Image Height="100" Width="100" Margin="12,0,9,0">
  <Image.Source>
    <BitmapImage UriSource="{Binding ImgURL}" CreateOptions="BackgroundCreation"/>
  </Image.Source>
</Image>

This MSDN blog post, old but still relevant, lists and explains all the CreationOptions and that caching is automatic in most modes.

I use these options to display many news items with images and it works well. I can load the list of articles, exit the app and turn Flight Mode to On, then start a new instance of the app and the images still load up.

Manual Approach

If you'd like to control the caching yourself and cache HTTPS resources then there are few good examples...

这篇关于图像源和缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆