Update MapBase.MapLayer.cs

This commit is contained in:
ClemensFischer 2025-08-12 23:02:11 +02:00
parent 21ac686843
commit ecd91faef2

View file

@ -1,5 +1,4 @@
using System; using System.Collections;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Linq; using System.Linq;
@ -38,8 +37,11 @@ namespace MapControl
/// <summary> /// <summary>
/// Gets or sets the base map layer, which is added as first element to the Children collection. /// Gets or sets the base map layer, which is added as first element to the Children collection.
/// If the layer implements IMapLayer (like MapTileLayer or MapImageLayer), its (non-null) MapBackground /// If the passed object is not a FrameworkElement, MapBase tries to locate a DataTemplate
/// and MapForeground property values are used for the MapBase Background and Foreground properties. /// resource for the object's type and generate a FrameworkElement from that DataTemplate.
/// If the FrameworkElement implements IMapLayer (like e.g. MapTileLayer or MapImageLayer),
/// its (non-null) MapBackground and MapForeground property values are used for the MapBase
/// Background and Foreground.
/// </summary> /// </summary>
public object MapLayer public object MapLayer
{ {
@ -55,12 +57,11 @@ namespace MapControl
private void MapLayerPropertyChanged(object oldLayer, object newLayer) private void MapLayerPropertyChanged(object oldLayer, object newLayer)
{ {
var firstChild = Children.Cast<FrameworkElement>().FirstOrDefault();
if (oldLayer != null) if (oldLayer != null)
{ {
if (firstChild != null && if (Children.Count > 0 &&
(firstChild == oldLayer as FrameworkElement || firstChild.DataContext == oldLayer)) (Children[0] == oldLayer as FrameworkElement ||
((FrameworkElement)Children[0]).DataContext == oldLayer))
{ {
Children.RemoveAt(0); Children.RemoveAt(0);
} }
@ -81,13 +82,14 @@ namespace MapControl
if (newLayer != null) if (newLayer != null)
{ {
if (firstChild == null || if (Children.Count == 0 ||
firstChild != newLayer as FrameworkElement && firstChild.DataContext != newLayer) (Children[0] != newLayer as FrameworkElement &&
((FrameworkElement)Children[0]).DataContext != newLayer))
{ {
Children.Insert(0, GetMapLayer(newLayer)); Children.Insert(0, GetMapLayer(newLayer));
} }
if (Children.Cast<FrameworkElement>().FirstOrDefault() is IMapLayer mapLayer) if (Children[0] is IMapLayer mapLayer)
{ {
if (mapLayer.MapBackground != null) if (mapLayer.MapBackground != null)
{ {