From d6de47a2ed974238665a022eb8eed69c89dc3e6a Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Fri, 5 Dec 2025 00:50:28 +0100 Subject: [PATCH] Updated DrawingTileMatrixLayer --- MapControl/WPF/DrawingTileMatrixLayer.cs | 62 +++++++----------------- 1 file changed, 18 insertions(+), 44 deletions(-) diff --git a/MapControl/WPF/DrawingTileMatrixLayer.cs b/MapControl/WPF/DrawingTileMatrixLayer.cs index dac741d1..347e7648 100644 --- a/MapControl/WPF/DrawingTileMatrixLayer.cs +++ b/MapControl/WPF/DrawingTileMatrixLayer.cs @@ -7,18 +7,19 @@ using System.Windows.Media; namespace MapControl { - public class ImageSourceTile(int zoomLevel, int x, int y, int columnCount) + public class ImageDrawingTile(int zoomLevel, int x, int y, int columnCount) : Tile(zoomLevel, x, y, columnCount) { - public event EventHandler Completed; - - public ImageSource ImageSource { get; set; } + public ImageDrawing Drawing { get; } = new ImageDrawing(); public override async Task LoadImageAsync(Func> loadImageFunc) { - ImageSource = await loadImageFunc().ConfigureAwait(false); + var image = await loadImageFunc().ConfigureAwait(false); - Completed?.Invoke(this, EventArgs.Empty); + if (image != null) + { + await Drawing.Dispatcher.InvokeAsync(() => Drawing.ImageSource = image); + } } } @@ -28,7 +29,7 @@ namespace MapControl public TileMatrix TileMatrix { get; private set; } = new TileMatrix(zoomLevel, 1, 1, 0, 0); - public IEnumerable Tiles { get; private set; } = []; + public IEnumerable Tiles { get; private set; } = []; public DrawingGroup Drawing { get; } = new DrawingGroup { Transform = new MatrixTransform() }; @@ -83,15 +84,15 @@ namespace MapControl TileMatrix = new TileMatrix(TileMatrix.ZoomLevel, xMin, yMin, xMax, yMax); - Drawing.Children.Clear(); CreateTiles(); + Drawing.Children = [.. Tiles.Select(tile => tile.Drawing)]; return true; } private void CreateTiles() { - var tiles = new List(); + var tiles = new List(); for (var y = TileMatrix.YMin; y <= TileMatrix.YMax; y++) { @@ -101,25 +102,22 @@ namespace MapControl if (tile == null) { - tile = new ImageSourceTile(TileMatrix.ZoomLevel, x, y, WmtsTileMatrix.MatrixWidth); + tile = new ImageDrawingTile(TileMatrix.ZoomLevel, x, y, WmtsTileMatrix.MatrixWidth); - var equivalentTile = Tiles.FirstOrDefault(t => t.ImageSource != null && t.Column == tile.Column && t.Row == tile.Row); + var equivalentTile = Tiles.FirstOrDefault(t => t.Drawing.ImageSource != null && t.Column == tile.Column && t.Row == tile.Row); if (equivalentTile != null) { tile.IsPending = false; - tile.ImageSource = equivalentTile.ImageSource; - } - else - { - tile.Completed += OnTileCompleted; + tile.Drawing.ImageSource = equivalentTile.Drawing.ImageSource; } } - if (tile.ImageSource != null) - { - DrawTile(tile); - } + tile.Drawing.Rect = new Rect( + WmtsTileMatrix.TileWidth * (x - TileMatrix.XMin), + WmtsTileMatrix.TileHeight * (y - TileMatrix.YMin), + WmtsTileMatrix.TileWidth, + WmtsTileMatrix.TileHeight); tiles.Add(tile); } @@ -127,29 +125,5 @@ namespace MapControl Tiles = tiles; } - - private void DrawTile(ImageSourceTile tile) - { - var width = WmtsTileMatrix.TileWidth; - var height = WmtsTileMatrix.TileHeight; - var x = width * (tile.X - TileMatrix.XMin); - var y = height * (tile.Y - TileMatrix.YMin); - - Drawing.Children.Add(new ImageDrawing(tile.ImageSource, new Rect(x, y, width, height))); - } - - private void OnTileCompleted(object sender, EventArgs e) - { - var tile = (ImageSourceTile)sender; - - tile.Completed -= OnTileCompleted; - - if (tile.X >= TileMatrix.XMin && tile.X <= TileMatrix.XMax && - tile.Y >= TileMatrix.YMin && tile.Y <= TileMatrix.YMax && - tile.ImageSource != null) - { - _ = Dispatcher.InvokeAsync(() => DrawTile(tile)); - } - } } }