From 14c918cd61c8b0a4bb95f034d054b9235c0b337f Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Fri, 24 Apr 2026 12:19:39 +0200 Subject: [PATCH] Update MapOverlaysPanel.cs --- MapControl/Shared/MapOverlaysPanel.cs | 132 +++++++++++--------------- 1 file changed, 55 insertions(+), 77 deletions(-) diff --git a/MapControl/Shared/MapOverlaysPanel.cs b/MapControl/Shared/MapOverlaysPanel.cs index 749bc27b..cb1728cc 100644 --- a/MapControl/Shared/MapOverlaysPanel.cs +++ b/MapControl/Shared/MapOverlaysPanel.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Linq; @@ -22,98 +23,75 @@ public partial class MapOverlaysPanel : MapPanel DependencyPropertyHelper.Register>(nameof(SourcePaths), null, async (control, oldValue, newValue) => await control.SourcePathsPropertyChanged(oldValue, newValue)); + private readonly UpdateTimer updateTimer; + + public MapOverlaysPanel() + { + updateTimer = new UpdateTimer { Interval = TimeSpan.FromMilliseconds(100) }; + updateTimer.Tick += async (_, _) => await UpdateChildren(); + } + public IEnumerable SourcePaths { get => (IEnumerable)GetValue(SourcePathsProperty); set => SetValue(SourcePathsProperty, value); } - private async Task SourcePathsPropertyChanged(IEnumerable oldSourcePaths, IEnumerable newSourcePaths) + protected virtual async Task CreateOverlayAsync(string sourcePath) { + var ext = Path.GetExtension(sourcePath).ToLower(); + + return ext == ".kmz" || ext == ".kml" + ? await GroundOverlay.CreateAsync(sourcePath) + : await GeoImage.CreateAsync(sourcePath); + } + + private async Task GetOverlay(string sourcePath) + { + var overlay = Children.Cast().FirstOrDefault(child => (string)child.Tag == sourcePath); + + if (overlay == null) + { + overlay = await CreateOverlayAsync(sourcePath); + overlay?.Tag = sourcePath; + } + + return overlay; + } + + private async Task UpdateChildren() + { + updateTimer.Stop(); + + var overlays = SourcePaths != null + ? await Task.WhenAll(SourcePaths.ToList().Select(GetOverlay)) + : []; + Children.Clear(); + foreach (var overlay in overlays.Where(overlay => overlay != null)) + { + Children.Add(overlay); + } + } + + private void SourcePathsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) + { + updateTimer.Run(true); + } + + private async Task SourcePathsPropertyChanged(IEnumerable oldSourcePaths, IEnumerable newSourcePaths) + { if (oldSourcePaths is INotifyCollectionChanged oldCollection) { oldCollection.CollectionChanged -= SourcePathsCollectionChanged; } - if (newSourcePaths != null) + if (newSourcePaths is INotifyCollectionChanged newCollection) { - if (newSourcePaths is INotifyCollectionChanged newCollection) - { - newCollection.CollectionChanged += SourcePathsCollectionChanged; - } - - await AddOverlays(0, newSourcePaths); - } - } - - private async void SourcePathsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) - { - switch (e.Action) - { - case NotifyCollectionChangedAction.Add: - await AddOverlays(e.NewStartingIndex, e.NewItems.Cast()); - break; - - case NotifyCollectionChangedAction.Remove: - RemoveOverlays(e.OldStartingIndex, e.OldItems.Count); - break; - - case NotifyCollectionChangedAction.Move: - RemoveOverlays(e.OldStartingIndex, e.OldItems.Count); - await AddOverlays(e.NewStartingIndex, e.NewItems.Cast()); - break; - - case NotifyCollectionChangedAction.Replace: - await ReplaceOverlays(e.NewStartingIndex, e.NewItems.Cast()); - break; - - case NotifyCollectionChangedAction.Reset: - Children.Clear(); - await AddOverlays(0, SourcePaths); - break; - } - } - - private async Task AddOverlays(int index, IEnumerable sourcePaths) - { - foreach (var sourcePath in sourcePaths) - { - Children.Insert(index++, await CreateOverlayAsync(sourcePath)); - } - } - - private async Task ReplaceOverlays(int index, IEnumerable sourcePaths) - { - foreach (var sourcePath in sourcePaths) - { - Children[index++] = await CreateOverlayAsync(sourcePath); - } - } - - private void RemoveOverlays(int index, int count) - { - while (--count >= 0) - { - Children.RemoveAt(index); - } - } - - protected virtual async Task CreateOverlayAsync(string sourcePath) - { - FrameworkElement overlay; - var ext = Path.GetExtension(sourcePath).ToLower(); - - if (ext == ".kmz" || ext == ".kml") - { - overlay = await GroundOverlay.CreateAsync(sourcePath); - } - else - { - overlay = await GeoImage.CreateAsync(sourcePath); + newCollection.CollectionChanged += SourcePathsCollectionChanged; } - return overlay; + await UpdateChildren(); } }