Minor cleanup in TileImageLoader

This commit is contained in:
ClemensF 2020-09-25 15:02:41 +02:00
parent ee4f049334
commit f5acb5f908
6 changed files with 13 additions and 28 deletions

View file

@ -70,14 +70,13 @@ namespace MapControl
}
}
internal static async Task<HttpResponse> GetHttpResponseAsync(Uri uri, bool continueOnCapturedContext = true)
internal static async Task<HttpResponse> GetHttpResponseAsync(Uri uri)
{
HttpResponse response = null;
try
{
using (var responseMessage = await HttpClient
.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(continueOnCapturedContext))
using (var responseMessage = await HttpClient.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false))
{
if (responseMessage.IsSuccessStatusCode)
{
@ -86,7 +85,7 @@ namespace MapControl
if (!responseMessage.Headers.TryGetValues("X-VE-Tile-Info", out IEnumerable<string> tileInfo) ||
!tileInfo.Contains("no-tile"))
{
buffer = await responseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(continueOnCapturedContext);
buffer = await responseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
}
response = new HttpResponse(buffer, responseMessage.Headers.CacheControl?.MaxAge);

View file

@ -108,11 +108,11 @@ namespace MapControl
/// <summary>
/// Loads a tile ImageSource asynchronously from GetUri(x, y, zoomLevel).
/// </summary>
public virtual async Task<ImageSource> LoadImageAsync(int x, int y, int zoomLevel)
public virtual Task<ImageSource> LoadImageAsync(int x, int y, int zoomLevel)
{
var uri = GetUri(x, y, zoomLevel);
return uri != null ? await ImageLoader.LoadImageAsync(uri) : null;
return uri != null ? ImageLoader.LoadImageAsync(uri) : Task.FromResult((ImageSource)null);
}
private string GetDefaultUri(int x, int y, int zoomLevel)

View file

@ -16,7 +16,7 @@ namespace MapControl
{
Pending = false;
if (fadeIn && MapBase.ImageFadeDuration > TimeSpan.Zero)
if (image != null && fadeIn && MapBase.ImageFadeDuration > TimeSpan.Zero)
{
if (image is BitmapImage bitmap && bitmap.UriSource != null)
{

View file

@ -35,7 +35,7 @@ namespace MapControl
if (cacheItem == null || cacheItem.Expiration < DateTime.UtcNow)
{
var response = await ImageLoader.GetHttpResponseAsync(uri, false).ConfigureAwait(false);
var response = await ImageLoader.GetHttpResponseAsync(uri).ConfigureAwait(false);
if (response != null) // download succeeded
{
@ -64,13 +64,7 @@ namespace MapControl
{
try
{
var image = await loadImageFunc();
if (image != null)
{
tile.SetImage(image);
}
tile.SetImage(await loadImageFunc());
tcs.SetResult(null);
}
catch (Exception ex)
@ -79,7 +73,7 @@ namespace MapControl
}
});
await tcs.Task.ConfigureAwait(false);
await tcs.Task.ConfigureAwait(false); // wait until image loading in the UI thread is completed
}
}
}

View file

@ -15,7 +15,7 @@ namespace MapControl
{
Pending = false;
if (fadeIn && MapBase.ImageFadeDuration > TimeSpan.Zero)
if (image != null && fadeIn && MapBase.ImageFadeDuration > TimeSpan.Zero)
{
if (image is BitmapSource bitmap && !bitmap.IsFrozen && bitmap.IsDownloading)
{

View file

@ -44,7 +44,7 @@ namespace MapControl
if (cacheItem == null || cacheItem.Expiration < DateTime.UtcNow)
{
var response = await ImageLoader.GetHttpResponseAsync(uri, false).ConfigureAwait(false);
var response = await ImageLoader.GetHttpResponseAsync(uri).ConfigureAwait(false);
if (response != null) // download succeeded
{
@ -58,7 +58,7 @@ namespace MapControl
{
var image = await ImageLoader.LoadImageAsync(buffer).ConfigureAwait(false);
SetTileImage(tile, image);
await tile.Image.Dispatcher.InvokeAsync(() => tile.SetImage(image));
}
}
@ -66,15 +66,7 @@ namespace MapControl
{
var image = await tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel).ConfigureAwait(false);
SetTileImage(tile, image);
}
private static void SetTileImage(Tile tile, ImageSource image)
{
if (image != null)
{
tile.Image.Dispatcher.InvokeAsync(() => tile.SetImage(image));
}
await tile.Image.Dispatcher.InvokeAsync(() => tile.SetImage(image));
}
private static Task<ImageCacheItem> GetCacheAsync(string cacheKey)