我正在开发一个应用程序,我希望用户从 Web 服务获取图像,但不希望它们存储在本地 localdb 或本地文件中。 有办法吗?
我正在使用 Xamarin 表单。但如果它可以本地完成,也可以通过 Xamarin 表单完成。
最佳答案
您始终可以使用网络客户端将图像下载为文件,然后使用 StreamImageSource。
首先,你需要一个依赖服务的接口(interface)(我更喜欢使用 WebClient 而不是任何第三方库,所以我使用这些服务,如果你喜欢可以随意使用其他任何东西,只需跳过服务部分)创建在表单项目:
public interface IDownloader
{
byte[] Download(string Url);
}
然后,你需要一个Android项目的依赖服务:
[assembly: Dependency ( typeof (--yournamespace--.Downloader))]
public class Downloader : IDownloader
{
public byte[] Download(string Url)
{
//This code is synchronous, I would recommend to do it asynchronously
WebClient wc = new WebClient();
return wc.DownloadData(Url);
}
}
然后您可以下载图像并将其设置为源:
//I assume there exist an image control called img.
var dl = DependencyService.Get<IDownloader> ();
byte[] data = dl.Download(--url to the image--);
var ms = new MemoryStream(data);
img.Source = new StreamImageSource{ Source = (t) => ms };
关于c# - Xamarin 表格 : how to receive images without storing anywhere locally?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40137116/