XAML-Map-Control/MapControl/WinUI/MapPanel.WinUI.cs

74 lines
2.3 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2024-02-03 21:01:53 +01:00
// Copyright © 2024 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
2024-05-20 23:24:34 +02:00
using System.Collections.Generic;
using System.Linq;
2024-05-22 11:25:32 +02:00
#if UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
2024-05-22 11:25:32 +02:00
#else
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
2021-06-14 21:41:37 +02:00
#endif
namespace MapControl
{
public partial class MapPanel
{
2021-01-17 00:31:30 +01:00
public MapPanel()
{
InitMapElement(this);
}
2017-09-05 20:57:17 +02:00
public static void InitMapElement(FrameworkElement element)
{
2017-09-05 20:57:17 +02:00
if (element is MapBase)
{
2017-09-05 20:57:17 +02:00
element.SetValue(ParentMapProperty, element);
}
else
{
2021-06-14 21:41:37 +02:00
// Workaround for missing property value inheritance.
2017-09-05 20:57:17 +02:00
// Loaded and Unloaded handlers set and clear the ParentMap property value.
2022-08-02 19:50:11 +02:00
element.Loaded += (s, e) => GetParentMap((FrameworkElement)s);
element.Unloaded += (s, e) => ((FrameworkElement)s).ClearValue(ParentMapProperty);
2017-09-05 20:57:17 +02:00
}
}
public static MapBase GetParentMap(FrameworkElement element)
{
var parentMap = (MapBase)element.GetValue(ParentMapProperty);
2024-05-20 23:24:34 +02:00
// Traverse visual tree because of missing property value inheritance.
2022-08-02 19:54:14 +02:00
if (parentMap == null &&
VisualTreeHelper.GetParent(element) is FrameworkElement parentElement)
{
2022-08-02 19:50:11 +02:00
parentMap = (parentElement as MapBase) ?? GetParentMap(parentElement);
if (parentMap != null)
{
element.SetValue(ParentMapProperty, parentMap);
}
}
return parentMap;
}
2024-05-20 23:24:34 +02:00
public static void SetRenderTransform(FrameworkElement element, Transform transform, double originX = 0d, double originY = 0d)
{
element.RenderTransform = transform;
element.RenderTransformOrigin = new Point(originX, originY);
}
protected IEnumerable<FrameworkElement> ChildElements => Children.OfType<FrameworkElement>();
2024-05-20 23:24:34 +02:00
private static void SetVisible(FrameworkElement element, bool visible)
{
2024-05-20 23:24:34 +02:00
element.Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
}
}
}