Ejemplo n.º 1
0
        private async Task <bool> DownloadTileImageAsync(Tile tile, Uri uri, string cacheKey)
        {
            var success = false;

            try
            {
                using (var response = await TileSource.HttpClient.GetAsync(uri))
                {
                    success = response.IsSuccessStatusCode;

                    if (!success)
                    {
                        Debug.WriteLine("TileImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
                    }
                    else if (TileSource.TileAvailable(response.Headers))
                    {
                        var buffer = await response.Content.ReadAsBufferAsync();

                        await SetTileImageAsync(tile, buffer); // create BitmapImage before caching

                        await Cache.SetAsync(cacheKey, buffer, GetExpiration(response));
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("TileImageLoader: {0}: {1}", uri, ex.Message);
            }

            return(success);
        }
Ejemplo n.º 2
0
        private async Task <bool> DownloadTileImageAsync(Tile tile, Uri uri, string cacheKey)
        {
            var success = false;

            try
            {
                using (var response = await TileSource.HttpClient.GetAsync(uri))
                {
                    success = response.IsSuccessStatusCode;

                    if (!success)
                    {
                        Debug.WriteLine("TileImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
                    }
                    else if (TileSource.TileAvailable(response.Headers))
                    {
                        using (var stream = new MemoryStream())
                        {
                            await response.Content.CopyToAsync(stream);

                            stream.Seek(0, SeekOrigin.Begin);

                            await SetTileImageAsync(tile, stream); // create BitmapFrame before caching

                            SetCachedImage(cacheKey, stream, GetExpiration(response));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("TileImageLoader: {0}: {1}", uri, ex.Message);
            }

            return(success);
        }