c# - Xamarin 表格 : how to receive images without storing anywhere locally?

标签 c# android ios xamarin xamarin.forms

我正在开发一个应用程序,我希望用户从 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/

相关文章:

c# - 使用 ImageSharp 调整色阶和颜色

android - Material 主题未正确应用于 pre-21 设备

android - 如何以编程方式从 Android 中的 Google 驱动器选择的文件下载并保存在本地目录中?

ios - 在类之间存储和传递数据的正确方法

c# - Mvvmlight 和 Xamarin 统一 API : Property not found

ios - UILabel:智能换行(动态)

c# - WPF 创建滑出面板

c# - 使用 MSBuild.exe 在 Release模式下构建 C# 解决方案

c# - 如何使用 MongoDB C# 序列化程序序列化值类型?

android - 如何处理 MediaPlayer 和 BaseAdapter