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

135 lines
4.2 KiB
C#
Raw Normal View History

2024-07-22 18:06:41 +02:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// Copyright © 2024 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
2024-08-30 16:37:40 +02:00
using System.IO;
2024-07-22 18:06:41 +02:00
using System.Linq;
#if WPF
using System.Windows;
#elif UWP
using Windows.UI.Xaml;
#elif WINUI
using Microsoft.UI.Xaml;
#endif
namespace MapControl
{
/// <summary>
/// A MapPanel with a collection of GroundOverlay or GeoImage children.
/// </summary>
public partial class MapOverlaysPanel : MapPanel
{
public static readonly DependencyProperty SourcePathsProperty =
DependencyPropertyHelper.Register<MapOverlaysPanel, IEnumerable<string>>(nameof(SourcePaths), null,
(control, oldValue, newValue) => control.SourcePathsPropertyChanged(oldValue, newValue));
public IEnumerable<string> SourcePaths
{
get => (IEnumerable<string>)GetValue(SourcePathsProperty);
set => SetValue(SourcePathsProperty, value);
}
private void SourcePathsPropertyChanged(IEnumerable<string> oldSourcePaths, IEnumerable<string> newSourcePaths)
{
Children.Clear();
if (oldSourcePaths is INotifyCollectionChanged oldCollection)
{
oldCollection.CollectionChanged -= SourcePathsCollectionChanged;
}
if (newSourcePaths != null)
{
if (newSourcePaths is INotifyCollectionChanged newCollection)
{
newCollection.CollectionChanged += SourcePathsCollectionChanged;
}
AddOverlays(0, newSourcePaths);
}
}
private void SourcePathsCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
AddOverlays(args.NewStartingIndex, args.NewItems.Cast<string>());
break;
case NotifyCollectionChangedAction.Remove:
RemoveOverlays(args.OldStartingIndex, args.OldItems.Count);
break;
case NotifyCollectionChangedAction.Move:
RemoveOverlays(args.OldStartingIndex, args.OldItems.Count);
AddOverlays(args.NewStartingIndex, args.NewItems.Cast<string>());
break;
case NotifyCollectionChangedAction.Replace:
ReplaceOverlays(args.NewStartingIndex, args.NewItems.Cast<string>());
break;
case NotifyCollectionChangedAction.Reset:
Children.Clear();
AddOverlays(0, SourcePaths);
break;
}
}
private void AddOverlays(int index, IEnumerable<string> sourcePaths)
{
foreach (var sourcePath in sourcePaths)
{
Children.Insert(index++, CreateOverlay(sourcePath));
}
}
private void ReplaceOverlays(int index, IEnumerable<string> sourcePaths)
{
foreach (var sourcePath in sourcePaths)
{
Children[index++] = CreateOverlay(sourcePath);
}
}
private void RemoveOverlays(int index, int count)
{
while (--count >= 0)
{
Children.RemoveAt(index);
}
}
protected virtual FrameworkElement CreateOverlay(string sourcePath)
{
FrameworkElement overlay;
2024-08-31 12:47:02 +02:00
var ext = Path.GetExtension(sourcePath).ToLower();
2024-07-22 18:06:41 +02:00
try
{
if (ext == ".kmz" || ext == ".kml")
{
overlay = new GroundOverlay { SourcePath = sourcePath };
}
else
{
overlay = new GeoImage { SourcePath = sourcePath };
}
}
catch (Exception ex)
{
2024-08-31 16:39:49 +02:00
Debug.WriteLine($"{nameof(MapOverlaysPanel)}: {sourcePath}: {ex.Message}");
2024-08-31 12:47:02 +02:00
2024-07-22 18:06:41 +02:00
overlay = new MapPanel();
}
return overlay;
}
}
}