diff --git a/MapControl/Shared/MapTileLayer.cs b/MapControl/Shared/MapTileLayer.cs index 2c710dd7..bd3df194 100644 --- a/MapControl/Shared/MapTileLayer.cs +++ b/MapControl/Shared/MapTileLayer.cs @@ -21,10 +21,7 @@ namespace MapControl { public interface ITileImageLoader { - TileSource TileSource { get; set; } - string SourceName { get; set; } - - void LoadTilesAsync(IEnumerable tiles); + void LoadTilesAsync(IEnumerable tiles, TileSource tileSource, string sourceName); } /// @@ -54,8 +51,7 @@ namespace MapControl new PropertyMetadata(null, (o, e) => ((MapTileLayer)o).TileSourcePropertyChanged())); public static readonly DependencyProperty SourceNameProperty = DependencyProperty.Register( - nameof(SourceName), typeof(string), typeof(MapTileLayer), - new PropertyMetadata(null, (o, e) => ((MapTileLayer)o).TileImageLoader.SourceName = (string)e.NewValue)); + nameof(SourceName), typeof(string), typeof(MapTileLayer), new PropertyMetadata(null)); public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register( nameof(Description), typeof(string), typeof(MapTileLayer), new PropertyMetadata(null)); @@ -275,8 +271,6 @@ namespace MapControl private void TileSourcePropertyChanged() { - TileImageLoader.TileSource = TileSource; - if (TileGrid != null) { Tiles = new List(); @@ -397,7 +391,7 @@ namespace MapControl Children.Add(tile.Image); } - TileImageLoader.LoadTilesAsync(Tiles); + TileImageLoader.LoadTilesAsync(Tiles, TileSource, SourceName); } } } diff --git a/MapControl/Shared/TileImageLoader.cs b/MapControl/Shared/TileImageLoader.cs index 9371d4d2..35a7ee56 100644 --- a/MapControl/Shared/TileImageLoader.cs +++ b/MapControl/Shared/TileImageLoader.cs @@ -48,33 +48,34 @@ namespace MapControl private readonly TileQueue tileQueue = new TileQueue(); private int taskCount; - public TileSource TileSource { get; set; } - public string SourceName { get; set; } - /// /// Loads all pending tiles from the tiles collection in up to MaxLoadTasks parallel Tasks. - /// If the UriFormat of TileSource starts with "http" and SourceName is a non-empty string, + /// If the UriFormat of the TileSource starts with "http" and sourceName is a non-empty string, /// tile images will be cached in the TileImageLoader's Cache. /// - public void LoadTilesAsync(IEnumerable tiles) + public void LoadTilesAsync(IEnumerable tiles, TileSource tileSource, string sourceName) { tileQueue.Clear(); - tileQueue.Enqueue(tiles); - var newTasks = Math.Min(tileQueue.Count, MaxLoadTasks) - taskCount; - - if (newTasks > 0) + if (tileSource != null) { - Interlocked.Add(ref taskCount, newTasks); + tileQueue.Enqueue(tiles); - while (--newTasks >= 0) + var newTasks = Math.Min(tileQueue.Count, MaxLoadTasks) - taskCount; + + if (newTasks > 0) { - Task.Run(() => LoadTilesFromQueueAsync()); + Interlocked.Add(ref taskCount, newTasks); + + while (--newTasks >= 0) + { + Task.Run(() => LoadTilesFromQueueAsync(tileSource, sourceName)); + } } } } - private async Task LoadTilesFromQueueAsync() + private async Task LoadTilesFromQueueAsync(TileSource tileSource, string sourceName) { Tile tile; @@ -82,7 +83,7 @@ namespace MapControl { try { - await LoadTileImageAsync(tile, TileSource, SourceName).ConfigureAwait(false); + await LoadTileImageAsync(tile, tileSource, sourceName).ConfigureAwait(false); } catch (Exception ex) { @@ -95,33 +96,30 @@ namespace MapControl private async Task LoadTileImageAsync(Tile tile, TileSource tileSource, string sourceName) { - if (tileSource != null) + if (Cache != null && + tileSource.UriFormat != null && + tileSource.UriFormat.StartsWith("http") && + !string.IsNullOrEmpty(sourceName)) { - if (Cache != null && - tileSource.UriFormat != null && - tileSource.UriFormat.StartsWith("http") && - !string.IsNullOrEmpty(sourceName)) - { - var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel); + var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel); - if (uri != null) + if (uri != null) + { + var extension = Path.GetExtension(uri.LocalPath); + + if (string.IsNullOrEmpty(extension) || extension == ".jpeg") { - var extension = Path.GetExtension(uri.LocalPath); - - if (string.IsNullOrEmpty(extension) || extension == ".jpeg") - { - extension = ".jpg"; - } - - var cacheKey = string.Format(CacheKeyFormat, sourceName, tile.ZoomLevel, tile.XIndex, tile.Y, extension); - - await LoadCachedTileImageAsync(tile, uri, cacheKey).ConfigureAwait(false); + extension = ".jpg"; } + + var cacheKey = string.Format(CacheKeyFormat, sourceName, tile.ZoomLevel, tile.XIndex, tile.Y, extension); + + await LoadCachedTileImageAsync(tile, uri, cacheKey).ConfigureAwait(false); } - else - { - await LoadTileImageAsync(tile, tileSource).ConfigureAwait(false); - } + } + else + { + await LoadTileImageAsync(tile, tileSource).ConfigureAwait(false); } }