From acdfc1861f1b820f199cfe3e20c08183020572bc Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Sun, 26 May 2024 12:39:40 +0200 Subject: [PATCH] Use StreamGeometry in WPF MapPolypoint --- MapControl/Avalonia/MapPolypoint.Avalonia.cs | 2 +- MapControl/WPF/MapPolypoint.WPF.cs | 50 +++++++++----------- MapControl/WPF/PolygonCollection.WPF.cs | 1 + MapControl/WinUI/MapPolypoint.WinUI.cs | 7 ++- 4 files changed, 27 insertions(+), 33 deletions(-) diff --git a/MapControl/Avalonia/MapPolypoint.Avalonia.cs b/MapControl/Avalonia/MapPolypoint.Avalonia.cs index ca2dcc65..37322d26 100644 --- a/MapControl/Avalonia/MapPolypoint.Avalonia.cs +++ b/MapControl/Avalonia/MapPolypoint.Avalonia.cs @@ -66,7 +66,7 @@ namespace MapControl InvalidateGeometry(); } - protected void AddPolylinePoints(PathFigures pathFigures, IEnumerable locations, double longitudeOffset, bool closed) + private void AddPolylinePoints(PathFigures pathFigures, IEnumerable locations, double longitudeOffset, bool closed) { if (locations.Count() >= 2) { diff --git a/MapControl/WPF/MapPolypoint.WPF.cs b/MapControl/WPF/MapPolypoint.WPF.cs index 258cf3a4..51f2ebc3 100644 --- a/MapControl/WPF/MapPolypoint.WPF.cs +++ b/MapControl/WPF/MapPolypoint.WPF.cs @@ -19,7 +19,7 @@ namespace MapControl { public static readonly DependencyProperty FillRuleProperty = DependencyPropertyHelper.Register(nameof(FillRule), FillRule.EvenOdd, - (polypoint, oldValue, newValue) => ((PathGeometry)polypoint.Data).FillRule = newValue); + (polypoint, oldValue, newValue) => ((StreamGeometry)polypoint.Data).FillRule = newValue); public FillRule FillRule { @@ -29,7 +29,7 @@ namespace MapControl protected MapPolypoint() { - Data = new PathGeometry(); + Data = new StreamGeometry(); } protected void DataCollectionPropertyChanged(IEnumerable oldValue, IEnumerable newValue) @@ -45,46 +45,47 @@ namespace MapControl } UpdateData(); + InvalidateVisual(); // necessary for StreamGeometry } bool IWeakEventListener.ReceiveWeakEvent(Type managerType, object sender, EventArgs e) { UpdateData(); + InvalidateVisual(); // necessary for StreamGeometry + return true; } protected void UpdateData(IEnumerable locations, bool closed) { - var pathFigures = new PathFigureCollection(); - - if (ParentMap != null && locations != null) + using (var context = ((StreamGeometry)Data).Open()) { - var longitudeOffset = GetLongitudeOffset(Location ?? locations.FirstOrDefault()); + if (ParentMap != null && locations != null) + { + var longitudeOffset = GetLongitudeOffset(Location ?? locations.FirstOrDefault()); - AddPolylinePoints(pathFigures, locations, longitudeOffset, closed); + AddPolylinePoints(context, locations, longitudeOffset, closed); + } } - - ((PathGeometry)Data).Figures = pathFigures; } protected void UpdateData(IEnumerable> polygons) { - var pathFigures = new PathFigureCollection(); - - if (ParentMap != null && polygons != null) + using (var context = ((StreamGeometry)Data).Open()) { - var longitudeOffset = GetLongitudeOffset(Location); - - foreach (var polygon in polygons) + if (ParentMap != null && polygons != null) { - AddPolylinePoints(pathFigures, polygon, longitudeOffset, true); + var longitudeOffset = GetLongitudeOffset(Location); + + foreach (var polygon in polygons) + { + AddPolylinePoints(context, polygon, longitudeOffset, true); + } } } - - ((PathGeometry)Data).Figures = pathFigures; } - protected void AddPolylinePoints(PathFigureCollection pathFigures, IEnumerable locations, double longitudeOffset, bool closed) + private void AddPolylinePoints(StreamGeometryContext context, IEnumerable locations, double longitudeOffset, bool closed) { if (locations.Count() >= 2) { @@ -93,15 +94,8 @@ namespace MapControl .Where(point => point.HasValue) .Select(point => point.Value); - var figure = new PathFigure - { - StartPoint = points.First(), - IsClosed = closed, - IsFilled = true - }; - - figure.Segments.Add(new PolyLineSegment(points.Skip(1), true)); - pathFigures.Add(figure); + context.BeginFigure(points.First(), true, closed); + context.PolyLineTo(points.Skip(1).ToList(), true, true); } } } diff --git a/MapControl/WPF/PolygonCollection.WPF.cs b/MapControl/WPF/PolygonCollection.WPF.cs index c63d4267..29df4c47 100644 --- a/MapControl/WPF/PolygonCollection.WPF.cs +++ b/MapControl/WPF/PolygonCollection.WPF.cs @@ -21,6 +21,7 @@ namespace MapControl public bool ReceiveWeakEvent(Type managerType, object sender, EventArgs e) { OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, sender, sender)); + return true; } diff --git a/MapControl/WinUI/MapPolypoint.WinUI.cs b/MapControl/WinUI/MapPolypoint.WinUI.cs index 7f58f542..a62f37c8 100644 --- a/MapControl/WinUI/MapPolypoint.WinUI.cs +++ b/MapControl/WinUI/MapPolypoint.WinUI.cs @@ -58,7 +58,8 @@ namespace MapControl protected void UpdateData(IEnumerable locations, bool closed) { - var pathFigures = new PathFigureCollection(); + var pathFigures = ((PathGeometry)Data).Figures; + pathFigures.Clear(); if (ParentMap != null && locations != null) { @@ -66,11 +67,9 @@ namespace MapControl AddPolylinePoints(pathFigures, locations, longitudeOffset, closed); } - - ((PathGeometry)Data).Figures = pathFigures; } - protected void AddPolylinePoints(PathFigureCollection pathFigures, IEnumerable locations, double longitudeOffset, bool closed) + private void AddPolylinePoints(PathFigureCollection pathFigures, IEnumerable locations, double longitudeOffset, bool closed) { if (locations.Count() >= 2) {