diff --git a/MapControl/Shared/Tile.cs b/MapControl/Shared/Tile.cs index b4b96ef6..cbb5fcc8 100644 --- a/MapControl/Shared/Tile.cs +++ b/MapControl/Shared/Tile.cs @@ -19,10 +19,11 @@ namespace MapControl public int Y { get; } = y; public int Column { get; } = ((x % columnCount) + columnCount) % columnCount; public int Row => Y; + public bool IsPending { get; set; } = true; /// - /// Runs a tile image download Task and marshals the result to the UI thread. + /// Runs a tile image download Task and passes the result to the UI thread. /// public abstract Task LoadImageAsync(Func> loadImageFunc); } diff --git a/MapControl/Shared/TileImageLoader.cs b/MapControl/Shared/TileImageLoader.cs index 4f1812f0..fb105de9 100644 --- a/MapControl/Shared/TileImageLoader.cs +++ b/MapControl/Shared/TileImageLoader.cs @@ -10,11 +10,14 @@ using System.Threading.Tasks; namespace MapControl { + /// + /// Loads and optionally caches map tile images for a MapTilePyramidLayer. + /// public interface ITileImageLoader { /// - /// Loads all pending tiles from the tiles collection. Tile image caching is enabled - /// when cacheName is a non-empty string and tiles are loaded from http or https Uris. + /// Starts asynchronous loading of all pending tiles from the tiles collection. + /// Caching is enabled when cacheName is a non-empty string and tiles are loaded from http or https Uris. /// void BeginLoadTiles(IEnumerable tiles, TileSource tileSource, string cacheName, IProgress progress); @@ -24,9 +27,6 @@ namespace MapControl void CancelLoadTiles(); } - /// - /// Loads and optionally caches map tile images for a MapTilePyramidLayer. - /// public class TileImageLoader : ITileImageLoader { private static ILogger logger; @@ -122,6 +122,7 @@ namespace MapControl if (tileQueue.Count > 0) { tile = tileQueue.Dequeue(); + tile.IsPending = false; return true; } @@ -135,35 +136,12 @@ namespace MapControl while (TryDequeueTile(out Tile tile)) { - tile.IsPending = false; - - Logger?.LogDebug("Thread {thread,2}: Loading tile ({zoom}/{column}/{row})", - Environment.CurrentManagedThreadId, tile.ZoomLevel, tile.Column, tile.Row); - try { - // Pass image loading callbacks to platform-specific method - // tile.LoadImageAsync(Func>) for completion in the UI thread. + Logger?.LogDebug("Thread {thread,2}: Loading tile ({zoom}/{column}/{row})", + Environment.CurrentManagedThreadId, tile.ZoomLevel, tile.Column, tile.Row); - var uri = tileSource.GetUri(tile.ZoomLevel, tile.Column, tile.Row); - - if (uri == null) - { - await tile.LoadImageAsync(() => tileSource.LoadImageAsync(tile.ZoomLevel, tile.Column, tile.Row)).ConfigureAwait(false); - } - else if (uri.Scheme != "http" && uri.Scheme != "https" || string.IsNullOrEmpty(cacheName)) - { - await tile.LoadImageAsync(() => ImageLoader.LoadImageAsync(uri)).ConfigureAwait(false); - } - else - { - var buffer = await LoadCachedBuffer(tile, uri, cacheName).ConfigureAwait(false); - - if (buffer != null) - { - await tile.LoadImageAsync(() => ImageLoader.LoadImageAsync(buffer)).ConfigureAwait(false); - } - } + await LoadTileImage(tile, tileSource, cacheName); } catch (Exception ex) { @@ -174,6 +152,32 @@ namespace MapControl } } + private static async Task LoadTileImage(Tile tile, TileSource tileSource, string cacheName) + { + // Pass image loading callbacks to platform-specific method + // tile.LoadImageAsync(Func>) for completion in the UI thread. + + var uri = tileSource.GetUri(tile.ZoomLevel, tile.Column, tile.Row); + + if (uri == null) + { + await tile.LoadImageAsync(() => tileSource.LoadImageAsync(tile.ZoomLevel, tile.Column, tile.Row)).ConfigureAwait(false); + } + else if (uri.Scheme != "http" && uri.Scheme != "https" || string.IsNullOrEmpty(cacheName)) + { + await tile.LoadImageAsync(() => ImageLoader.LoadImageAsync(uri)).ConfigureAwait(false); + } + else + { + var buffer = await LoadCachedBuffer(tile, uri, cacheName).ConfigureAwait(false); + + if (buffer != null) + { + await tile.LoadImageAsync(() => ImageLoader.LoadImageAsync(buffer)).ConfigureAwait(false); + } + } + } + private static async Task LoadCachedBuffer(Tile tile, Uri uri, string cacheName) { var extension = Path.GetExtension(uri.LocalPath).ToLower();