【问题标题】:How to download image to Xamarin.forms listview如何将图像下载到 Xamarin.forms 列表视图
【发布时间】:2018-09-12 20:01:02
【问题描述】:

我在列表视图中有一个图片 url,点击到列表视图后,图片应该下载到同一行。 如何在 Xamarin.forms 上实现? 我从 Azure blob 获得图像

【问题讨论】:

  • 您能否添加更多详细信息,例如屏幕截图或其他信息以帮助您?

标签: azure xamarin.forms azure-blob-storage


【解决方案1】:

我建议使用带有 SQL 和 Blob 存储的 Xamarin Forms Listview 查看这篇博文。他们基本上使用 SQL 使用以下代码存储 Blob 存储中的图像:

public class AzureMediaStorageService : IMediaStorageService
{
    private CloudBlobContainer MediaContainer { get; set; }



    public void InitializeStorage(string settings = null)
    {
        if (MediaContainer != null)
            return;

        if (string.IsNullOrEmpty(settings))
            throw new ArgumentNullException(nameof(settings), "Azure Storage Media Service needs a connection string name as \"settings\" parameter");

        var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings[settings].ConnectionString);
        var blobClient = storageAccount.CreateCloudBlobClient();

        MediaContainer = blobClient.GetContainerReference("medias");
        MediaContainer.CreateIfNotExists();
        MediaContainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
    }


    public async Task<Uri> UploadBytesAsync(byte[] bytes, string uniqueId, MediaType mediaType)
    {
        var blockBlob = MediaContainer.GetBlockBlobReference(uniqueId);

        blockBlob.Properties.ContentType = mediaType == MediaType.Picture ? "image/jpeg" : "video/mp4";

        await blockBlob.UploadFromByteArrayAsync(bytes, 0, bytes.Length);

        return blockBlob.Uri;
    }

    public async Task<byte[]> DownloadBytesAsync(Uri uri)
    {
        using (var httpClient = new HttpClient())
        {
            var response = await httpClient.GetAsync(uri, HttpCompletionOption.ResponseContentRead);

            response.EnsureSuccessStatusCode();

            return await response.Content.ReadAsByteArrayAsync();
        }
    }

    public string GetSecureClientUploadUrl(string uniqueId, DateTimeOffset expirationDateTime)
    {
        if (expirationDateTime <= DateTimeOffset.Now)
            throw new ArgumentOutOfRangeException(nameof(expirationDateTime), expirationDateTime, null);

        var blob = MediaContainer.GetBlockBlobReference(uniqueId);

        var sasPolicy = new SharedAccessBlobPolicy
        {
            SharedAccessExpiryTime = expirationDateTime,
            Permissions = SharedAccessBlobPermissions.Create
        };


        return MediaContainer.Uri + blob.GetSharedAccessSignature(sasPolicy);
    }
}

可在此处找到博文:https://forums.xamarin.com/discussion/98784/how-to-load-images-in-xamarin-forms-listview-with-asp-net-web-api-from-sql-azure-database

【讨论】:

    猜你喜欢
    • 2018-12-23
    • 1970-01-01
    • 2011-04-07
    • 1970-01-01
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    相关资源
    最近更新 更多