【发布时间】:2015-06-27 21:13:37
【问题描述】:
我正在使用 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)
};
}
我们的图像处理程序使用 GUID 从我们的 api 检索 svg 图像,调整大小并重新着色,然后返回请求的格式,例如.png它旨在缓存图像并在我们的网站上运行良好。
我的问题是图像没有缓存在移动设备或模拟器上。关于我可以做些什么来解决这个问题的任何想法?
谢谢,
贷款
【问题讨论】:
-
嘿,你知道如何缓存图像以供离线使用吗?我遇到了同样的问题!谢谢!目前我正在为图像使用 FFImageLoading 库。
标签: c# image xamarin.forms offline-caching