XAML-Map-Control/MapControl/Shared/MapOverlaysPanel.cs

120 lines
3.5 KiB
C#
Raw Normal View History

2025-03-31 21:33:52 +02:00
using System.Collections.Generic;
2024-07-22 18:06:41 +02:00
using System.Collections.Specialized;
2024-08-30 16:37:40 +02:00
using System.IO;
2024-07-22 18:06:41 +02:00
using System.Linq;
using System.Threading.Tasks;
2024-07-22 18:06:41 +02:00
#if WPF
using System.Windows;
#elif UWP
using Windows.UI.Xaml;
#elif WINUI
using Microsoft.UI.Xaml;
#endif
2026-04-13 17:14:49 +02:00
namespace MapControl;
/// <summary>
/// A MapPanel with a collection of GroundOverlay or GeoImage children.
/// </summary>
public partial class MapOverlaysPanel : MapPanel
2024-07-22 18:06:41 +02:00
{
2026-04-13 17:14:49 +02:00
public static readonly DependencyProperty SourcePathsProperty =
DependencyPropertyHelper.Register<MapOverlaysPanel, IEnumerable<string>>(nameof(SourcePaths), null,
async (control, oldValue, newValue) => await control.SourcePathsPropertyChanged(oldValue, newValue));
public IEnumerable<string> SourcePaths
{
get => (IEnumerable<string>)GetValue(SourcePathsProperty);
set => SetValue(SourcePathsProperty, value);
}
private async Task SourcePathsPropertyChanged(IEnumerable<string> oldSourcePaths, IEnumerable<string> newSourcePaths)
2024-07-22 18:06:41 +02:00
{
2026-04-13 17:14:49 +02:00
Children.Clear();
2024-07-22 18:06:41 +02:00
2026-04-13 17:14:49 +02:00
if (oldSourcePaths is INotifyCollectionChanged oldCollection)
2024-07-22 18:06:41 +02:00
{
2026-04-13 17:14:49 +02:00
oldCollection.CollectionChanged -= SourcePathsCollectionChanged;
2024-07-22 18:06:41 +02:00
}
2026-04-13 17:14:49 +02:00
if (newSourcePaths != null)
2024-07-22 18:06:41 +02:00
{
2026-04-13 17:14:49 +02:00
if (newSourcePaths is INotifyCollectionChanged newCollection)
2024-07-22 18:06:41 +02:00
{
2026-04-13 17:14:49 +02:00
newCollection.CollectionChanged += SourcePathsCollectionChanged;
2024-07-22 18:06:41 +02:00
}
2026-04-13 17:14:49 +02:00
await AddOverlays(0, newSourcePaths);
2024-07-22 18:06:41 +02:00
}
2026-04-13 17:14:49 +02:00
}
2024-07-22 18:06:41 +02:00
2026-04-13 17:14:49 +02:00
private async void SourcePathsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
2024-07-22 18:06:41 +02:00
{
2026-04-13 17:14:49 +02:00
case NotifyCollectionChangedAction.Add:
await AddOverlays(e.NewStartingIndex, e.NewItems.Cast<string>());
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<string>());
break;
case NotifyCollectionChangedAction.Replace:
await ReplaceOverlays(e.NewStartingIndex, e.NewItems.Cast<string>());
break;
case NotifyCollectionChangedAction.Reset:
Children.Clear();
await AddOverlays(0, SourcePaths);
break;
2024-07-22 18:06:41 +02:00
}
2026-04-13 17:14:49 +02:00
}
2024-07-22 18:06:41 +02:00
2026-04-13 17:14:49 +02:00
private async Task AddOverlays(int index, IEnumerable<string> sourcePaths)
{
foreach (var sourcePath in sourcePaths)
2024-07-22 18:06:41 +02:00
{
2026-04-13 17:14:49 +02:00
Children.Insert(index++, await CreateOverlayAsync(sourcePath));
2024-07-22 18:06:41 +02:00
}
2026-04-13 17:14:49 +02:00
}
2024-07-22 18:06:41 +02:00
2026-04-13 17:14:49 +02:00
private async Task ReplaceOverlays(int index, IEnumerable<string> sourcePaths)
{
foreach (var sourcePath in sourcePaths)
2024-07-22 18:06:41 +02:00
{
2026-04-13 17:14:49 +02:00
Children[index++] = await CreateOverlayAsync(sourcePath);
2024-07-22 18:06:41 +02:00
}
2026-04-13 17:14:49 +02:00
}
2024-07-22 18:06:41 +02:00
2026-04-13 17:14:49 +02:00
private void RemoveOverlays(int index, int count)
{
while (--count >= 0)
2024-07-22 18:06:41 +02:00
{
2026-04-13 17:14:49 +02:00
Children.RemoveAt(index);
2024-07-22 18:06:41 +02:00
}
2026-04-13 17:14:49 +02:00
}
2024-07-22 18:06:41 +02:00
2026-04-13 17:14:49 +02:00
protected virtual async Task<FrameworkElement> CreateOverlayAsync(string sourcePath)
{
FrameworkElement overlay;
var ext = Path.GetExtension(sourcePath).ToLower();
2024-07-22 18:06:41 +02:00
2026-04-13 17:14:49 +02:00
if (ext == ".kmz" || ext == ".kml")
{
overlay = await GroundOverlay.CreateAsync(sourcePath);
}
else
{
overlay = await GeoImage.CreateAsync(sourcePath);
2024-07-22 18:06:41 +02:00
}
2026-04-13 17:14:49 +02:00
return overlay;
2024-07-22 18:06:41 +02:00
}
}