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

99 lines
2.4 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;
#else
using Avalonia.Metadata;
using FrameworkElement = Avalonia.Controls.Control;
#endif
namespace MapControl.UiTools
{
#if WPF
[ContentProperty(nameof(MapLayer))]
#elif UWP || WINUI
[ContentProperty(Name = nameof(MapLayer))]
#endif
public partial class MapLayerMenuItem : MapMenuItem
{
#if AVALONIA
[Content]
#endif
2025-09-06 12:25:47 +02:00
public FrameworkElement MapLayer { get; set; }
2025-09-20 14:02:42 +02:00
protected override bool GetIsChecked(MapBase map)
{
2025-09-08 17:47:57 +02:00
return MapLayer != null && map.Children.Contains(MapLayer);
}
2025-09-18 14:36:02 +02:00
public override Task ExecuteAsync(MapBase map)
{
2025-09-06 12:25:47 +02:00
if (MapLayer != null)
2025-03-21 18:03:57 +01:00
{
2025-09-06 12:25:47 +02:00
map.MapLayer = MapLayer;
2025-03-21 18:03:57 +01:00
}
2025-09-18 14:36:02 +02:00
return Task.CompletedTask;
}
}
public partial class MapOverlayMenuItem : MapLayerMenuItem
{
2025-09-18 14:36:02 +02:00
public string SourcePath { get; set; }
2025-09-17 10:04:34 +02:00
public int InsertOrder { get; set; }
public override async Task ExecuteAsync(MapBase map)
2025-09-17 10:04:34 +02:00
{
2025-09-18 14:36:02 +02:00
if (MapLayer == null)
{
await CreateMapLayer();
}
2025-09-06 12:25:47 +02:00
if (MapLayer != null)
{
2025-09-06 12:25:47 +02:00
if (map.Children.Contains(MapLayer))
{
2025-09-06 12:25:47 +02:00
map.Children.Remove(MapLayer);
2025-03-21 18:48:33 +01:00
}
else
{
2025-09-08 17:47:57 +02:00
var insertIndex = ParentMenuItems
2025-03-21 18:48:33 +01:00
.OfType<MapOverlayMenuItem>()
2025-09-08 17:47:57 +02:00
.Where(item => item.InsertOrder <= InsertOrder && item.GetIsChecked(map))
.Count();
2025-03-21 18:48:33 +01:00
2025-09-08 17:47:57 +02:00
if (map.MapLayer != null)
{
insertIndex++;
}
2025-09-08 17:47:57 +02:00
map.Children.Insert(insertIndex, MapLayer);
}
2025-03-21 18:48:33 +01:00
}
}
2025-09-18 14:36:02 +02:00
protected virtual async Task CreateMapLayer()
{
var ext = Path.GetExtension(SourcePath).ToLower();
if (ext == ".kmz" || ext == ".kml")
{
MapLayer = await GroundOverlay.CreateAsync(SourcePath);
}
else
{
MapLayer = await GeoImage.CreateAsync(SourcePath);
}
}
}
}