Using MvxImageViewLoader to load a resource image,

2019-05-22 23:35发布

问题:

I'm using mvvmcross binding in a MvxTableViewCell, the cell elements are an UIImageView and 2 labels.

These "rows"/cells will list a set of predefined elements, the image of these elements are png files that exist inside the Resources directory (bundle resource) or at any place on the internet.

Since I might have this mix of images that exist in the bundle and on the internet I would like to use MvxImageViewLoader.

The view model has a list of objects that contain a property that is the image uri, for the resource images I tried several solutions: - NSBundle.MainBundle.ResourcePath+"/xpto.png" - "file://" + NSBundle.MainBundle.ResourcePath+"/xpto.png"

All the images that are located in the internet work without any problem.

Is it possible to use MvxImageViewLoader to load images that exist in the resource folder? Should I use a converter?

Thanks for your help,

Luis Pinho

回答1:

The MvvmCross DownloadCache plugin on Touch resolves image loading to: MvxTouchLocalFileImageLoader - see https://github.com/slodge/MvvmCross/blob/v3/Plugins/Cirrious/DownloadCache/Cirrious.MvvmCross.Plugins.DownloadCache.Touch/MvxTouchLocalFileImageLoader.cs

This uses res: as the scheme/prefix to detect whether a resource file has been requestd and then uses UIImage.FromFile to load them.

Since other platforms do not support resource files using the same prefix, you may find you need to use a value converter for this when moving to multiple platforms.

Alternatively, you could easily implement your own MyImageView control inheriting from MvxImageView which would intercept the bound property and decide whether to download it or to display it from resources.