繁体   English   中英

Xamarin.Forms缓存图像

[英]Xamarin.Forms caching images

我正在使用Xamarin.forms构建一个在IOS,Windows Phone和Android上运行的应用程序。 当没有可用的网络时,我需要能够在设备上缓存图像。

基础:

我正在使用MVVM方法并且具有带有IconImageId属性的MenuViewModel,该属性具有针对不同图像的GUID。

然后,我视图单元格中的图标将绑定到此属性:

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

然后我使用名为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)
    };      
}

我们的Image处理程序使用GUID从我们的api中检索svg图像,调整大小并重新计算它,然后返回所请求的格式,例如png。 它旨在缓存图像,并在我们的网站上运行良好。

我的问题是图像不是在移动设备上缓存,也不是在模拟器上缓存。 关于我可以做些什么来解决这个问题的任何想法?

谢谢,

贷款

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

暂无
暂无

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

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