c# - Xamarin.Forms 缓存图像

标签 c# image xamarin.forms offline-caching

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

基础知识:

我正在使用 MVVM 方法,并且有一个 MenuViewModel 和一个 IconImageId 属性,它有一个用于不同图像的 GUID。

我的 View 单元格中的图标然后绑定(bind)到此属性:

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

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

我的问题是图像没有缓存在移动设备或模拟器上。关于我可以做些什么来解决这个问题有什么想法吗?

谢谢,

贷款

最佳答案

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

关于c# - Xamarin.Forms 缓存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29759845/

相关文章:

c# - session 或 webAPI 中的任何替代方案

c# - 如何在不更改 C#.Net 中的分辨率的情况下裁剪图像?

ios - 如何在 iOS 中裁剪到信箱

xamarin - 带有位置(纬度和经度)的 Xamarin.Forms (PCL) WebView

c# - 我如何在表单上从 iOS 和 Android 打开 UIViewController

c# - 如果我在 MongoDB 上使用 LINQ,为什么会失去性能?

c# - 从串行端口读取时如何应用编码

c# - 将 Windows XP-8 下的音频设备访问限制为特定应用程序/用户

image - 使用/不同的过滤器在 MATLAB 中调整大小

sqlite - 如何在Xamarin表单中使用SqLite?