Avalonia MapPath

This commit is contained in:
ClemensFischer 2024-05-25 18:58:51 +02:00
parent 8e4110b600
commit 25d4d7f417
8 changed files with 120 additions and 11 deletions

View file

@ -56,10 +56,18 @@ namespace MapControl
}
public static StyledProperty<TValue> AddOwner<TOwner, TValue>(
StyledProperty<TValue> property)
StyledProperty<TValue> property,
Action<TOwner, TValue, TValue> changed = null)
where TOwner : AvaloniaObject
{
return property.AddOwner<TOwner>();
var newProperty = property.AddOwner<TOwner>();
if (changed != null)
{
newProperty.Changed.AddClassHandler<TOwner, TValue>((o, e) => changed(o, e.OldValue.Value, e.NewValue.Value));
}
return newProperty;
}
public static StyledProperty<TValue> AddOwner<TOwner, TValue>(

View file

@ -112,7 +112,7 @@ namespace MapControl
/// </summary>
public double ViewScale
{
get => (double)GetValue(ViewScaleProperty);
get => GetValue(ViewScaleProperty);
private set => RaisePropertyChanged(ViewScaleProperty, double.NaN, value);
}

View file

@ -28,7 +28,6 @@
<Compile Remove="..\Shared\MapGraticule.cs" />
<Compile Remove="..\Shared\MapItem.cs" />
<Compile Remove="..\Shared\MapItemsControl.cs" />
<Compile Remove="..\Shared\MapPath.cs" />
<Compile Remove="..\Shared\MapPolyline.cs" />
<Compile Remove="..\Shared\MapPolygon.cs" />
<Compile Remove="..\Shared\ViewTransform.cs" />

View file

@ -0,0 +1,101 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// Copyright © 2024 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using Avalonia.Controls.Shapes;
using Avalonia.Media;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
namespace MapControl
{
public partial class MapPath : Shape
{
public MapPath()
{
Stretch = Stretch.None;
}
public static readonly StyledProperty<Geometry> DataProperty =
DependencyPropertyHelper.AddOwner<MapPath, Geometry>(Path.DataProperty,
(path, oldValue, newValue) => path.DataPropertyChanged(oldValue, newValue));
public Geometry Data
{
get => GetValue(DataProperty);
set => SetValue(DataProperty, value);
}
protected override Geometry CreateDefiningGeometry() => Data;
private void DataPropertyChanged(Geometry oldData, Geometry newData)
{
// Check if data is actually a new Geometry.
//
if (newData != null && !ReferenceEquals(newData, oldData))
{
UpdateData();
}
}
private void SetMapTransform(Matrix matrix)
{
if (Data.Transform is MatrixTransform transform)
{
transform.Matrix = matrix;
}
else
{
Data.Transform = new MatrixTransform(matrix);
}
}
#region Methods used only by derived classes MapPolyline, MapPolygon and MapMultiPolygon
protected void DataCollectionPropertyChanged(IEnumerable oldValue, IEnumerable newValue)
{
if (oldValue is INotifyCollectionChanged oldCollection)
{
oldCollection.CollectionChanged -= DataCollectionChanged;
}
if (newValue is INotifyCollectionChanged newCollection)
{
newCollection.CollectionChanged += DataCollectionChanged;
}
UpdateData();
}
protected void DataCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdateData();
}
protected void AddPolylinePoints(PathFigures pathFigures, IEnumerable<Location> locations, double longitudeOffset, bool closed)
{
if (locations.Count() >= 2)
{
var points = locations
.Select(location => LocationToView(location, longitudeOffset))
.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)));
pathFigures.Add(figure);
}
}
#endregion
}
}