Xamarin形式:图像缓存

Son*_*ali 4 c# portable-class-library memorycache xamarin.forms

嗨,我正在尝试使用xamarin表单PCL构建应用程序。我正在实现使用默认图像的图像库。所有图像都在斑点上。我要下载图像并将该图像缓存在设备中,下载完成后,我需要用它替换默认图像。下次加载应用程序时,仅当图像不在缓存中时才下载图像。我没有任何用于图像缓存和从缓存加载图像的插件。我见过一个名为FFPLUGIN的插件,但是没有用。知道如何实施吗?图像训练

use*_*er1 6

您可以使用内置ImageCaching的Xamarin表单,如下所示:

https://developer.xamarin.com/guides/xamarin-forms/working-with/images/#Downloaded_Image_Caching

下载图像缓存

UriImageSource还支持通过以下属性配置的已下载图像的缓存:

CachingEnabled-是否启用缓存(默认为true)。

CacheValidity-一个TimeSpan,它定义图像在本地存储多长时间。默认情况下,缓存处于启用状态,它将在本地存储图像24小时。要禁用特定图像的缓存,请实例化图像源,如下所示:

Image.Source = new UriImageSource {CachingEnabled = false,
Uri="http://server.com/image"}; To set a specific cache period (for
example, 5 days) instantiate the image source like this:


webImage.Source = new UriImageSource {
Uri = new Uri("https://xamarin.com/content/images/pages/forms/example-app.png"),
CachingEnabled = true,
CacheValidity = new TimeSpan(5,0,0,0) };
Run Code Online (Sandbox Code Playgroud)

内置缓存使支持滚动图像列表等场景变得非常容易,您可以在其中设置(或绑定)每个单元格中的图像,并让内置缓存负责在滚动单元格时重新加载图像回到视野。