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

136 lines
4.3 KiB
C#
Raw Permalink Normal View History

2025-02-27 18:46:32 +01:00
using Windows.Foundation;
2022-04-27 18:00:40 +02:00
using System.Collections.Generic;
using System.Linq;
2024-05-22 11:25:32 +02:00
#if UWP
2024-05-26 20:32:29 +02:00
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
2024-05-26 21:25:36 +02:00
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
2024-05-22 11:25:32 +02:00
#else
2024-05-26 20:32:29 +02:00
using Microsoft.UI.Xaml;
2024-05-22 11:25:32 +02:00
using Microsoft.UI.Xaml.Controls;
2024-05-26 21:25:36 +02:00
using Microsoft.UI.Xaml.Data;
2024-05-22 11:25:32 +02:00
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Shapes;
2021-06-14 21:41:37 +02:00
#endif
namespace MapControl
{
2024-05-26 20:32:29 +02:00
public partial class MapGraticule : MapPanel
{
2024-05-26 20:32:29 +02:00
public static readonly DependencyProperty ForegroundProperty =
DependencyPropertyHelper.Register<MapGraticule, Brush>(nameof(Foreground));
public static readonly DependencyProperty FontFamilyProperty =
DependencyPropertyHelper.Register<MapGraticule, FontFamily>(nameof(FontFamily));
public static readonly DependencyProperty FontSizeProperty =
DependencyPropertyHelper.Register<MapGraticule, double>(nameof(FontSize), 12d);
protected override void SetParentMap(MapBase map)
{
if (map != null && Foreground == null)
{
2024-05-26 21:25:36 +02:00
SetBinding(ForegroundProperty,
new Binding { Source = map, Path = new PropertyPath(nameof(Foreground)) });
2024-05-26 20:32:29 +02:00
}
base.SetParentMap(map);
}
protected override void OnViewportChanged(ViewportChangedEventArgs e)
{
2025-06-10 09:13:48 +02:00
Path path;
2022-04-27 18:00:40 +02:00
if (Children.Count == 0)
{
2025-06-10 09:13:48 +02:00
path = new Path { Data = new PathGeometry() };
2024-05-26 21:25:36 +02:00
path.SetBinding(Shape.StrokeProperty,
new Binding { Source = this, Path = new PropertyPath(nameof(Foreground)) });
path.SetBinding(Shape.StrokeThicknessProperty,
new Binding { Source = this, Path = new PropertyPath(nameof(StrokeThickness)) });
2022-04-27 18:00:40 +02:00
Children.Add(path);
}
2025-06-10 09:13:48 +02:00
else
{
path = (Path)Children[0];
}
2025-06-10 09:13:48 +02:00
var labels = DrawGraticule(((PathGeometry)path.Data).Figures);
2022-04-27 18:00:40 +02:00
var childrenCount = 1;
2022-04-27 18:00:40 +02:00
foreach (var label in labels)
{
TextBlock textBlock;
2022-04-27 18:00:40 +02:00
if (childrenCount < Children.Count)
{
2022-04-27 18:00:40 +02:00
textBlock = (TextBlock)Children[childrenCount];
}
else
{
textBlock = new TextBlock { RenderTransform = new MatrixTransform() };
2024-05-26 21:25:36 +02:00
textBlock.SetBinding(TextBlock.FontSizeProperty,
new Binding { Source = this, Path = new PropertyPath(nameof(FontSize)) });
textBlock.SetBinding(TextBlock.ForegroundProperty,
new Binding { Source = this, Path = new PropertyPath(nameof(Foreground)) });
2022-04-27 18:00:40 +02:00
if (FontFamily != null)
{
2024-05-26 21:25:36 +02:00
textBlock.SetBinding(TextBlock.FontFamilyProperty,
new Binding { Source = this, Path = new PropertyPath(nameof(FontFamily)) });
2022-04-27 18:00:40 +02:00
}
2022-04-27 18:00:40 +02:00
Children.Add(textBlock);
}
2022-04-27 18:00:40 +02:00
textBlock.Text = label.LatitudeText + "\n" + label.LongitudeText;
textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
2022-04-27 18:00:40 +02:00
var matrix = new Matrix(1, 0, 0, 1, 0, 0);
2022-04-27 18:00:40 +02:00
matrix.Translate(StrokeThickness / 2d + 2d, -textBlock.DesiredSize.Height / 2d);
matrix.Rotate(label.Rotation);
2022-04-28 18:34:30 +02:00
matrix.Translate(label.X, label.Y);
2022-04-27 18:00:40 +02:00
((MatrixTransform)textBlock.RenderTransform).Matrix = matrix;
2022-04-27 18:00:40 +02:00
childrenCount++;
}
2022-04-27 18:00:40 +02:00
while (Children.Count > childrenCount)
{
Children.RemoveAt(Children.Count - 1);
}
2022-04-27 18:00:40 +02:00
base.OnViewportChanged(e);
}
2022-04-28 18:34:30 +02:00
private static PathFigure CreatePolylineFigure(IEnumerable<Point> points)
2022-04-27 18:00:40 +02:00
{
var figure = new PathFigure
{
StartPoint = points.First(),
2024-05-29 12:02:55 +02:00
IsClosed = false,
2022-04-27 18:00:40 +02:00
IsFilled = false
};
2022-04-27 18:00:40 +02:00
var polyline = new PolyLineSegment();
2022-04-27 18:00:40 +02:00
foreach (var p in points.Skip(1))
{
2022-04-27 18:00:40 +02:00
polyline.Points.Add(p);
}
2022-04-27 18:00:40 +02:00
figure.Segments.Add(polyline);
return figure;
}
}
}