XAML-Map-Control/MapUiTools/Shared/MapLayerMenuItem.cs

102 lines
2.3 KiB
C#
Raw Normal View History

2025-09-18 14:36:02 +02:00
using System.IO;
using System.Linq;
using System.Threading.Tasks;
#if WPF
using System.Windows;
using System.Windows.Markup;
#elif UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Markup;
#elif WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Markup;
2026-04-10 16:55:33 +02:00
#elif AVALONIA
using Avalonia.Metadata;
using FrameworkElement = Avalonia.Controls.Control;
#endif
2026-04-13 17:14:49 +02:00
namespace MapControl.UiTools;
#if WPF
2026-04-13 17:14:49 +02:00
[ContentProperty(nameof(MapLayer))]
#elif UWP || WINUI
2026-04-13 17:14:49 +02:00
[ContentProperty(Name = nameof(MapLayer))]
#endif
2026-04-13 17:14:49 +02:00
public partial class MapLayerMenuItem : MapMenuItem
{
#if AVALONIA
2026-04-13 17:14:49 +02:00
[Content]
#endif
2026-04-13 17:14:49 +02:00
public FrameworkElement MapLayer { get; set; }
2026-04-13 17:14:49 +02:00
protected override bool GetIsChecked(MapBase map)
{
return MapLayer != null && map.Children.Contains(MapLayer);
}
2026-04-13 17:14:49 +02:00
public override Task ExecuteAsync(MapBase map)
{
if (MapLayer != null)
{
2026-04-13 17:14:49 +02:00
map.MapLayer = MapLayer;
}
2026-04-13 17:14:49 +02:00
return Task.CompletedTask;
}
2026-04-13 17:14:49 +02:00
}
2026-04-13 17:14:49 +02:00
public partial class MapOverlayMenuItem : MapLayerMenuItem
{
public string SourcePath { get; set; }
public int InsertOrder { get; set; }
2025-09-18 14:36:02 +02:00
2026-04-13 17:14:49 +02:00
public double OverlayOpacity { get; set; } = 1d;
2025-09-17 10:04:34 +02:00
2026-04-13 17:14:49 +02:00
public override async Task ExecuteAsync(MapBase map)
{
if (MapLayer == null)
{
await CreateMapLayer();
}
2026-01-26 19:00:57 +01:00
2026-04-13 17:14:49 +02:00
if (MapLayer != null)
2025-09-17 10:04:34 +02:00
{
2026-04-13 17:14:49 +02:00
if (map.Children.Contains(MapLayer))
2025-09-18 14:36:02 +02:00
{
2026-04-13 17:14:49 +02:00
map.Children.Remove(MapLayer);
2025-09-18 14:36:02 +02:00
}
2026-04-13 17:14:49 +02:00
else
{
2026-04-13 17:14:49 +02:00
var insertIndex = ParentMenuItems
.OfType<MapOverlayMenuItem>()
.Where(item => item.InsertOrder <= InsertOrder && item.GetIsChecked(map))
.Count();
if (map.MapLayer != null)
{
2026-04-13 17:14:49 +02:00
insertIndex++;
2025-03-21 18:48:33 +01:00
}
2025-09-08 17:47:57 +02:00
2026-04-13 17:14:49 +02:00
map.Children.Insert(insertIndex, MapLayer);
2025-03-21 18:48:33 +01:00
}
}
2026-04-13 17:14:49 +02:00
}
2025-09-18 14:36:02 +02:00
2026-04-13 17:14:49 +02:00
protected virtual async Task CreateMapLayer()
{
var ext = Path.GetExtension(SourcePath).ToLower();
2026-01-26 19:00:57 +01:00
2026-04-13 17:14:49 +02:00
if (ext == ".kmz" || ext == ".kml")
{
MapLayer = await GroundOverlay.CreateAsync(SourcePath);
}
else
{
MapLayer = await GeoImage.CreateAsync(SourcePath);
2025-09-18 14:36:02 +02:00
}
2026-04-13 17:14:49 +02:00
MapLayer.Opacity = OverlayOpacity;
}
}