Exemple #1
0
        private async Task LoadCachedTileImageAsync(Tile tile, Uri uri, string cacheKey)
        {
            ImageSource image = null;
            DateTime    expiration;
            var         cacheBuffer = GetCachedImage(cacheKey, out expiration);

            if (cacheBuffer == null || expiration < DateTime.UtcNow)
            {
                var response = await ImageLoader.LoadHttpStreamAsync(uri).ConfigureAwait(false);

                if (response != null)            // download succeeded
                {
                    cacheBuffer = null;          // discard cached image

                    if (response.Stream != null) // tile image available
                    {
                        using (var stream = response.Stream)
                        {
                            image = ImageLoader.LoadImage(stream);
                            SetCachedImage(cacheKey, stream, GetExpiration(response.MaxAge));
                        }
                    }
                }
            }

            if (cacheBuffer != null) // cached image not expired or download failed
            {
                image = ImageLoader.LoadImage(cacheBuffer);
            }

            if (image != null)
            {
                SetTileImage(tile, image);
            }
        }
        private async Task LoadCachedTileImageAsync(Tile tile, Uri uri, string cacheKey)
        {
            DateTime expiration;
            var      cacheBuffer = GetCachedImage(cacheKey, out expiration);

            if (cacheBuffer == null || expiration < DateTime.UtcNow)
            {
                var result = await ImageLoader.LoadHttpStreamAsync(uri);

                if (result != null)           // download succeeded
                {
                    cacheBuffer = null;       // discard cached image

                    if (result.Item1 != null) // tile image available
                    {
                        using (var stream = result.Item1)
                        {
                            LoadTileImage(tile, stream);
                            SetCachedImage(cacheKey, stream, GetExpiration(result.Item2));
                        }
                    }
                }
            }

            if (cacheBuffer != null)
            {
                using (var stream = new MemoryStream(cacheBuffer))
                {
                    LoadTileImage(tile, stream);
                }
            }
        }