mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
58 lines
2 KiB
C#
58 lines
2 KiB
C#
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
|
// © 2021 Clemens Fischer
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
using System;
|
|
using System.Runtime.InteropServices.WindowsRuntime;
|
|
using System.Threading.Tasks;
|
|
using Windows.Storage;
|
|
|
|
namespace MapControl
|
|
{
|
|
public partial class TileImageLoader
|
|
{
|
|
/// <summary>
|
|
/// Default StorageFolder where an IImageCache instance may save cached data,
|
|
/// i.e. ApplicationData.Current.TemporaryFolder.
|
|
/// </summary>
|
|
public static StorageFolder DefaultCacheFolder
|
|
{
|
|
get { return ApplicationData.Current.TemporaryFolder; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// The IImageCache implementation used to cache tile images. The default is null.
|
|
/// </summary>
|
|
public static Caching.IImageCache Cache { get; set; }
|
|
|
|
|
|
private static async Task LoadCachedTileAsync(Tile tile, Uri uri, string cacheKey)
|
|
{
|
|
var cacheItem = await Cache.GetAsync(cacheKey).ConfigureAwait(false);
|
|
var buffer = cacheItem?.Buffer;
|
|
|
|
if (cacheItem == null || cacheItem.Expiration < DateTime.UtcNow)
|
|
{
|
|
var response = await ImageLoader.GetHttpResponseAsync(uri).ConfigureAwait(false);
|
|
|
|
if (response != null) // download succeeded
|
|
{
|
|
buffer = response.Buffer?.AsBuffer(); // may be null or empty when no tile available, but still be cached
|
|
|
|
await Cache.SetAsync(cacheKey, buffer, GetExpiration(response.MaxAge)).ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
if (buffer != null && buffer.Length > 0)
|
|
{
|
|
await tile.SetImageAsync(() => ImageLoader.LoadImageAsync(buffer)).ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
private static Task LoadTileAsync(Tile tile, TileSource tileSource)
|
|
{
|
|
return tile.SetImageAsync(() => tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel));
|
|
}
|
|
}
|
|
}
|