简体   繁体   English

Xamarin.Forms缓存图像

[英]Xamarin.Forms caching images

I am using Xamarin.forms to build an application which runs on IOS, Windows Phone and Android. 我正在使用Xamarin.forms构建一个在IOS,Windows Phone和Android上运行的应用程序。 I need to be able cache images on the device when there is no network available. 当没有可用的网络时,我需要能够在设备上缓存图像。

The basics: 基础:

I'm using an MVVM approach and have a MenuViewModel with a IconImageId property which has a GUID for different images. 我正在使用MVVM方法并且具有带有IconImageId属性的MenuViewModel,该属性具有针对不同图像的GUID。

The Icons in my view cell then binds to this property: 然后,我视图单元格中的图标将绑定到此属性:

var icon = new Image
{
    Aspect = Aspect.AspectFit,
    HeightRequest = 80, 
    WidthRequest = 80       
};
icon.SetBinding(Image.SourceProperty, new Binding("IconImageId",BindingMode.Default, new ImageIdToUriImageSourceValueConverter(80, 80, iconColor)));

I then use a custom Value Converter called ImageIdToUriImageSourceValueConverter to convert my image Id to the image: 然后我使用名为ImageIdToUriImageSourceValueConverter的自定义值转换器将我的图像Id转换为图像:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    var applicationUrl = App.ApplicationUrl;
    if (string.IsNullOrEmpty(value as string))
    {
        return null;
    }

    var includeColorOld = string.IsNullOrEmpty(_colorOld) ? "" : string.Format("/{0}", _colorOld);

    // I have an image api which which handles the image itself and that works fine.
    var uriString = string.Format("{0}/img/{1}/{2}{3}/{4}/{5}/{6}", applicationUrl, _x, _y, includeColorOld, _color, value, "image.png");

    return = new UriImageSource
    {
        Uri = new Uri(uriString),
        CachingEnabled = true,
        CacheValidity = new TimeSpan(30, 0, 0, 0)
    };      
}

Our Image handler uses the GUID to retrieve a svg image from our api, resizes and recolours it then returns is on the requested format eg png. 我们的Image处理程序使用GUID从我们的api中检索svg图像,调整大小并重新计算它,然后返回所请求的格式,例如png。 It designed to caches the images and works very well in our websites. 它旨在缓存图像,并在我们的网站上运行良好。

My problem is that images are not caching on the mobile devices nor on the emulators. 我的问题是图像不是在移动设备上缓存,也不是在模拟器上缓存。 Any ideas on what I can do to solve this issues? 关于我可以做些什么来解决这个问题的任何想法?

Thanks, 谢谢,

Lóan 贷款

您可以尝试https://github.com/molinch/FFImageLoading ,它是具有更好缓存功能的图像替换(Android,iOS,Windows)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM