From 21ac68684383c70ca839ac8ade030907c86df761 Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Tue, 12 Aug 2025 11:33:39 +0200 Subject: [PATCH] MapLayer/MapLayerItemsSource --- MapControl/Avalonia/MapPanel.Avalonia.cs | 16 +------------ MapControl/Shared/MapBase.MapLayer.cs | 27 +++++++++++++-------- MapControl/Shared/MapPanel.cs | 7 +++--- MapControl/WPF/MapPanel.WPF.cs | 22 +---------------- MapControl/WinUI/MapPanel.WinUI.cs | 30 +----------------------- 5 files changed, 24 insertions(+), 78 deletions(-) diff --git a/MapControl/Avalonia/MapPanel.Avalonia.cs b/MapControl/Avalonia/MapPanel.Avalonia.cs index d794ff5d..36f6f056 100644 --- a/MapControl/Avalonia/MapPanel.Avalonia.cs +++ b/MapControl/Avalonia/MapPanel.Avalonia.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; - -namespace MapControl +namespace MapControl { public partial class MapPanel { @@ -13,18 +11,6 @@ namespace MapControl public static readonly AttachedProperty BoundingBoxProperty = DependencyPropertyHelper.RegisterAttached("BoundingBox", typeof(MapPanel)); - protected Controls ChildElements => Children; - - protected FrameworkElement GetChildElement(int index) => index < Children.Count ? Children[index] : null; - - protected void InsertChildElement(int index, FrameworkElement element) => Children.Insert(index, element); - - protected void InsertChildElements(int index, IEnumerable elements) => Children.InsertRange(index, elements); - - protected void RemoveChildElement(int index) => Children.RemoveAt(index); - - protected void RemoveChildElements(int index, int count) => Children.RemoveRange(index, count); - static MapPanel() { AffectsParentArrange(LocationProperty, BoundingBoxProperty); diff --git a/MapControl/Shared/MapBase.MapLayer.cs b/MapControl/Shared/MapBase.MapLayer.cs index 9eb48214..c418874c 100644 --- a/MapControl/Shared/MapBase.MapLayer.cs +++ b/MapControl/Shared/MapBase.MapLayer.cs @@ -1,4 +1,5 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; @@ -54,14 +55,14 @@ namespace MapControl private void MapLayerPropertyChanged(object oldLayer, object newLayer) { - var firstChild = GetChildElement(0); + var firstChild = Children.Cast().FirstOrDefault(); if (oldLayer != null) { if (firstChild != null && (firstChild == oldLayer as FrameworkElement || firstChild.DataContext == oldLayer)) { - RemoveChildElement(0); + Children.RemoveAt(0); } if (hasMapLayerBackground) @@ -83,10 +84,10 @@ namespace MapControl if (firstChild == null || firstChild != newLayer as FrameworkElement && firstChild.DataContext != newLayer) { - InsertChildElement(0, GetMapLayer(newLayer)); + Children.Insert(0, GetMapLayer(newLayer)); } - if (GetChildElement(0) is IMapLayer mapLayer) + if (Children.Cast().FirstOrDefault() is IMapLayer mapLayer) { if (mapLayer.MapBackground != null) { @@ -168,17 +169,23 @@ namespace MapControl private void AddMapLayers(List mapLayers, int index) { - InsertChildElements(index, mapLayers); - - if (index == 0) + foreach (var mapLayer in mapLayers) { - MapLayer = mapLayers[0]; + Children.Insert(index, mapLayer); + + if (index == 0) + { + MapLayer = mapLayer; + } } } private void RemoveMapLayers(IEnumerable items, int index) { - RemoveChildElements(index, items.Cast().Count()); + foreach (var _ in items) + { + Children.RemoveAt(index); + } if (index == 0) { diff --git a/MapControl/Shared/MapPanel.cs b/MapControl/Shared/MapPanel.cs index ccc0f9cb..b7240084 100644 --- a/MapControl/Shared/MapPanel.cs +++ b/MapControl/Shared/MapPanel.cs @@ -1,4 +1,5 @@ -#if WPF +using System.Linq; +#if WPF using System.Windows; using System.Windows.Controls; using System.Windows.Media; @@ -172,7 +173,7 @@ namespace MapControl { availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity); - foreach (var element in ChildElements) + foreach (var element in Children.Cast()) { element.Measure(availableSize); } @@ -184,7 +185,7 @@ namespace MapControl { if (parentMap != null) { - foreach (var element in ChildElements) + foreach (var element in Children.Cast()) { ArrangeChildElement(element, finalSize); } diff --git a/MapControl/WPF/MapPanel.WPF.cs b/MapControl/WPF/MapPanel.WPF.cs index d3cfec0c..63a65a03 100644 --- a/MapControl/WPF/MapPanel.WPF.cs +++ b/MapControl/WPF/MapPanel.WPF.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; -using System.Linq; -using System.Windows; +using System.Windows; using System.Windows.Media; namespace MapControl @@ -18,24 +16,6 @@ namespace MapControl DependencyPropertyHelper.RegisterAttached("BoundingBox", typeof(MapPanel), null, FrameworkPropertyMetadataOptions.AffectsParentArrange); - protected IEnumerable ChildElements => Children.OfType(); - - protected FrameworkElement GetChildElement(int index) => index < Children.Count ? (FrameworkElement)Children[index] : null; - - protected void InsertChildElement(int index, FrameworkElement element) => Children.Insert(index, element); - - protected void InsertChildElements(int index, IEnumerable elements) - { - foreach (var element in elements) - { - Children.Insert(index++, element); - } - } - - protected void RemoveChildElement(int index) => Children.RemoveAt(index); - - protected void RemoveChildElements(int index, int count) => Children.RemoveRange(index, count); - public static MapBase GetParentMap(FrameworkElement element) { return (MapBase)element.GetValue(ParentMapProperty); diff --git a/MapControl/WinUI/MapPanel.WinUI.cs b/MapControl/WinUI/MapPanel.WinUI.cs index 18b9022e..b15196b4 100644 --- a/MapControl/WinUI/MapPanel.WinUI.cs +++ b/MapControl/WinUI/MapPanel.WinUI.cs @@ -1,8 +1,4 @@ -using System.Collections.Generic; -using System.Linq; -using System.Xml.Linq; - -#if UWP +#if UWP using Windows.UI.Xaml; using Windows.UI.Xaml.Media; #else @@ -25,30 +21,6 @@ namespace MapControl DependencyPropertyHelper.RegisterAttached("BoundingBox", typeof(MapPanel), null, (element, oldValue, newValue) => (element.Parent as MapPanel)?.InvalidateArrange()); - protected IEnumerable ChildElements => Children.OfType(); - - protected FrameworkElement GetChildElement(int index) => index < Children.Count ? (FrameworkElement)Children[index] : null; - - protected void InsertChildElement(int index, FrameworkElement element) => Children.Insert(index, element); - - protected void InsertChildElements(int index, IEnumerable elements) - { - foreach (var element in elements) - { - Children.Insert(index++, element); - } - } - - protected void RemoveChildElement(int index) => Children.RemoveAt(index); - - protected void RemoveChildElements(int index, int count) - { - while (--count >= 0) - { - RemoveChildElement(index); - } - } - public static void InitMapElement(FrameworkElement element) { // Workaround for missing property value inheritance.