From be427c195a46d252bc10d17b58fd79cc38ba6a69 Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Sat, 27 Dec 2025 21:24:01 +0100 Subject: [PATCH] C#14 field backed properties --- MBTiles/Shared/MBTileLayer.cs | 3 +- MBTiles/Shared/MBTileSource.cs | 3 +- MBTiles/WPF/MBTiles.WPF.csproj | 2 +- MapControl/Avalonia/MapGraticule.Avalonia.cs | 16 +++++----- MapControl/Shared/GeoImage.cs | 3 +- MapControl/Shared/GroundOverlay.cs | 3 +- MapControl/Shared/ImageLoader.cs | 20 ++++++++----- MapControl/Shared/MapItem.cs | 31 +++++++++----------- MapControl/Shared/MapPath.cs | 30 +++++++++---------- MapControl/Shared/MapProjection.cs | 6 ++-- MapControl/Shared/TileImageLoader.cs | 3 +- MapControl/Shared/TilePyramidLayer.cs | 20 ++++++------- MapControl/Shared/UriTileSource.cs | 9 +++--- MapControl/Shared/WmsImageLayer.cs | 3 +- MapControl/Shared/WmtsTileLayer.cs | 3 +- MapControl/WPF/MapControl.WPF.csproj | 2 +- MapControl/WPF/MapGraticule.WPF.cs | 16 +++++----- MapProjections/Shared/GeoApiProjection.cs | 18 +++++------- MapProjections/WPF/MapProjections.WPF.csproj | 2 +- MapUiTools/WPF/MapUiTools.WPF.csproj | 2 +- 20 files changed, 89 insertions(+), 106 deletions(-) diff --git a/MBTiles/Shared/MBTileLayer.cs b/MBTiles/Shared/MBTileLayer.cs index 4353ff71..74b76a40 100644 --- a/MBTiles/Shared/MBTileLayer.cs +++ b/MBTiles/Shared/MBTileLayer.cs @@ -18,8 +18,7 @@ namespace MapControl.MBTiles /// public class MBTileLayer : MapTileLayer { - private static ILogger logger; - private static ILogger Logger => logger ??= ImageLoader.LoggerFactory?.CreateLogger(); + private static ILogger Logger => field ??= ImageLoader.LoggerFactory?.CreateLogger(); public static readonly DependencyProperty FileProperty = DependencyPropertyHelper.Register(nameof(File), null, diff --git a/MBTiles/Shared/MBTileSource.cs b/MBTiles/Shared/MBTileSource.cs index bddbdcb5..d640f9e8 100644 --- a/MBTiles/Shared/MBTileSource.cs +++ b/MBTiles/Shared/MBTileSource.cs @@ -17,8 +17,7 @@ namespace MapControl.MBTiles { public sealed class MBTileSource : TileSource, IDisposable { - private static ILogger logger; - private static ILogger Logger => logger ??= ImageLoader.LoggerFactory?.CreateLogger(); + private static ILogger Logger => field ??= ImageLoader.LoggerFactory?.CreateLogger(); private SQLiteConnection connection; diff --git a/MBTiles/WPF/MBTiles.WPF.csproj b/MBTiles/WPF/MBTiles.WPF.csproj index b7e1e29e..1a60ac2a 100644 --- a/MBTiles/WPF/MBTiles.WPF.csproj +++ b/MBTiles/WPF/MBTiles.WPF.csproj @@ -1,7 +1,7 @@  net10.0-windows;net462 - 12.0 + 14.0 true WPF MapControl.MBTiles diff --git a/MapControl/Avalonia/MapGraticule.Avalonia.cs b/MapControl/Avalonia/MapGraticule.Avalonia.cs index 1dda6e08..5d05aaf7 100644 --- a/MapControl/Avalonia/MapGraticule.Avalonia.cs +++ b/MapControl/Avalonia/MapGraticule.Avalonia.cs @@ -24,26 +24,24 @@ namespace MapControl public static readonly StyledProperty FontSizeProperty = TextElement.FontSizeProperty.AddOwner(new StyledPropertyMetadata(12d)); - private MapBase parentMap; - /// /// Implements IMapElement.ParentMap. /// public MapBase ParentMap { - get => parentMap; + get; set { - if (parentMap != null) + if (field != null) { - parentMap.ViewportChanged -= OnViewportChanged; + field.ViewportChanged -= OnViewportChanged; } - parentMap = value; + field = value; - if (parentMap != null) + if (field != null) { - parentMap.ViewportChanged += OnViewportChanged; + field.ViewportChanged += OnViewportChanged; } } } @@ -55,7 +53,7 @@ namespace MapControl public override void Render(DrawingContext drawingContext) { - if (parentMap != null) + if (ParentMap != null) { var pathGeometry = new PathGeometry(); diff --git a/MapControl/Shared/GeoImage.cs b/MapControl/Shared/GeoImage.cs index f1049824..0057833d 100644 --- a/MapControl/Shared/GeoImage.cs +++ b/MapControl/Shared/GeoImage.cs @@ -62,8 +62,7 @@ namespace MapControl private static string QueryString(ushort tag) => $"/ifd/{{ushort={tag}}}"; - private static ILogger logger; - private static ILogger Logger => logger ??= ImageLoader.LoggerFactory?.CreateLogger(typeof(GeoImage)); + private static ILogger Logger => field ??= ImageLoader.LoggerFactory?.CreateLogger(typeof(GeoImage)); public static readonly DependencyProperty SourcePathProperty = DependencyPropertyHelper.RegisterAttached("SourcePath", typeof(GeoImage), null, diff --git a/MapControl/Shared/GroundOverlay.cs b/MapControl/Shared/GroundOverlay.cs index bdc52d39..f3150b2b 100644 --- a/MapControl/Shared/GroundOverlay.cs +++ b/MapControl/Shared/GroundOverlay.cs @@ -66,8 +66,7 @@ namespace MapControl } } - private static ILogger logger; - private static ILogger Logger => logger ??= ImageLoader.LoggerFactory?.CreateLogger(typeof(GroundOverlay)); + private static ILogger Logger => field ??= ImageLoader.LoggerFactory?.CreateLogger(typeof(GroundOverlay)); public static readonly DependencyProperty SourcePathProperty = DependencyPropertyHelper.Register(nameof(SourcePath), null, diff --git a/MapControl/Shared/ImageLoader.cs b/MapControl/Shared/ImageLoader.cs index c2587c71..d6c6fffb 100644 --- a/MapControl/Shared/ImageLoader.cs +++ b/MapControl/Shared/ImageLoader.cs @@ -17,20 +17,26 @@ namespace MapControl { public static partial class ImageLoader { - private static ILogger logger; - private static ILogger Logger => logger ??= LoggerFactory?.CreateLogger(typeof(ImageLoader)); + private static ILogger Logger => field ??= LoggerFactory?.CreateLogger(typeof(ImageLoader)); public static ILoggerFactory LoggerFactory { get; set; } /// /// The System.Net.Http.HttpClient instance used to download images. /// - public static HttpClient HttpClient { get; set; } - - static ImageLoader() + public static HttpClient HttpClient { - HttpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(10) }; - HttpClient.DefaultRequestHeaders.Add("User-Agent", $"XAML-Map-Control/{typeof(ImageLoader).Assembly.GetName().Version}"); + get + { + if (field == null) + { + field = new HttpClient { Timeout = TimeSpan.FromSeconds(10) }; + field.DefaultRequestHeaders.Add("User-Agent", $"XAML-Map-Control/{typeof(ImageLoader).Assembly.GetName().Version}"); + } + + return field; + } + set; } public static bool IsHttp(this Uri uri) diff --git a/MapControl/Shared/MapItem.cs b/MapControl/Shared/MapItem.cs index 8af6c689..eff1c54b 100644 --- a/MapControl/Shared/MapItem.cs +++ b/MapControl/Shared/MapItem.cs @@ -19,9 +19,6 @@ namespace MapControl /// public partial class MapItem : ListBoxItem, IMapElement { - private MapBase parentMap; - private MatrixTransform mapTransform; - /// /// Gets/sets MapPanel.AutoCollapse. /// @@ -45,21 +42,21 @@ namespace MapControl /// public MapBase ParentMap { - get => parentMap; + get; set { - if (parentMap != null) + if (field != null) { - parentMap.ViewportChanged -= OnViewportChanged; + field.ViewportChanged -= OnViewportChanged; } - parentMap = value; + field = value; - if (parentMap != null && mapTransform != null) + if (field != null && MapTransform != null) { // Attach ViewportChanged handler only if MapTransform is actually used. // - parentMap.ViewportChanged += OnViewportChanged; + field.ViewportChanged += OnViewportChanged; UpdateMapTransform(); } @@ -70,23 +67,23 @@ namespace MapControl /// Gets a Transform for scaling and rotating geometries /// in map coordinates (meters) to view coordinates (pixels). /// - public Transform MapTransform + public MatrixTransform MapTransform { get { - if (mapTransform == null) + if (field == null) { - mapTransform = new MatrixTransform(); + field = new MatrixTransform(); - if (parentMap != null) + if (ParentMap != null) { - parentMap.ViewportChanged += OnViewportChanged; + ParentMap.ViewportChanged += OnViewportChanged; UpdateMapTransform(); } } - return mapTransform; + return field; } } @@ -97,9 +94,9 @@ namespace MapControl private void UpdateMapTransform() { - if (mapTransform != null && parentMap != null && Location != null) + if (MapTransform != null && ParentMap != null && Location != null) { - mapTransform.Matrix = parentMap.GetMapTransform(Location); + MapTransform.Matrix = ParentMap.GetMapTransform(Location); } } } diff --git a/MapControl/Shared/MapPath.cs b/MapControl/Shared/MapPath.cs index b4e0dbc5..8c0901af 100644 --- a/MapControl/Shared/MapPath.cs +++ b/MapControl/Shared/MapPath.cs @@ -20,8 +20,6 @@ namespace MapControl DependencyPropertyHelper.Register(nameof(Location), null, (path, oldValue, newValue) => path.UpdateData()); - private MapBase parentMap; - /// /// Gets or sets a Location that is used as /// - either the origin point of a geometry specified in projected map coordinates (meters) @@ -40,19 +38,19 @@ namespace MapControl /// public MapBase ParentMap { - get => parentMap; + get; set { - if (parentMap != null) + if (field != null) { - parentMap.ViewportChanged -= OnViewportChanged; + field.ViewportChanged -= OnViewportChanged; } - parentMap = value; + field = value; - if (parentMap != null) + if (field != null) { - parentMap.ViewportChanged += OnViewportChanged; + field.ViewportChanged += OnViewportChanged; } UpdateData(); @@ -66,9 +64,9 @@ namespace MapControl protected virtual void UpdateData() { - if (parentMap != null && Location != null && Data != null) + if (ParentMap != null && Location != null && Data != null) { - SetDataTransform(parentMap.GetMapTransform(Location)); + SetDataTransform(ParentMap.GetMapTransform(Location)); } MapPanel.SetLocation(this, Location); @@ -78,13 +76,13 @@ namespace MapControl { var longitudeOffset = 0d; - if (parentMap.MapProjection.Type <= MapProjectionType.NormalCylindrical && location != null) + if (ParentMap.MapProjection.Type <= MapProjectionType.NormalCylindrical && location != null) { - var position = parentMap.LocationToView(location); + var position = ParentMap.LocationToView(location); - if (position.HasValue && !parentMap.InsideViewBounds(position.Value)) + if (position.HasValue && !ParentMap.InsideViewBounds(position.Value)) { - longitudeOffset = parentMap.CoerceLongitude(location.Longitude) - location.Longitude; + longitudeOffset = ParentMap.CoerceLongitude(location.Longitude) - location.Longitude; } } @@ -93,7 +91,7 @@ namespace MapControl protected Point? LocationToMap(Location location, double longitudeOffset) { - var point = parentMap.MapProjection.LocationToMap(location.Latitude, location.Longitude + longitudeOffset); + var point = ParentMap.MapProjection.LocationToMap(location.Latitude, location.Longitude + longitudeOffset); if (point.HasValue) { @@ -116,7 +114,7 @@ namespace MapControl if (point.HasValue) { - point = parentMap.ViewTransform.MapToViewMatrix.Transform(point.Value); + point = ParentMap.ViewTransform.MapToViewMatrix.Transform(point.Value); } return point; diff --git a/MapControl/Shared/MapProjection.cs b/MapControl/Shared/MapProjection.cs index 8f06ac5d..8a2aaea9 100644 --- a/MapControl/Shared/MapProjection.cs +++ b/MapControl/Shared/MapProjection.cs @@ -31,12 +31,10 @@ namespace MapControl public const double Wgs84Flattening = 1d / 298.257223563; public static readonly double Wgs84Eccentricity = Math.Sqrt((2d - Wgs84Flattening) * Wgs84Flattening); - private static MapProjectionFactory factory; - public static MapProjectionFactory Factory { - get => factory ??= new MapProjectionFactory(); - set => factory = value; + get => field ??= new MapProjectionFactory(); + set; } /// diff --git a/MapControl/Shared/TileImageLoader.cs b/MapControl/Shared/TileImageLoader.cs index 5acb1519..438df2fe 100644 --- a/MapControl/Shared/TileImageLoader.cs +++ b/MapControl/Shared/TileImageLoader.cs @@ -29,8 +29,7 @@ namespace MapControl public class TileImageLoader : ITileImageLoader { - private static ILogger logger; - private static ILogger Logger => logger ??= ImageLoader.LoggerFactory?.CreateLogger(typeof(TileImageLoader)); + private static ILogger Logger => field ??= ImageLoader.LoggerFactory?.CreateLogger(typeof(TileImageLoader)); /// /// Default folder path where a persistent cache implementation may save data, i.e. "C:\ProgramData\MapControl\TileCache". diff --git a/MapControl/Shared/TilePyramidLayer.cs b/MapControl/Shared/TilePyramidLayer.cs index a1b2393d..8ca34fcf 100644 --- a/MapControl/Shared/TilePyramidLayer.cs +++ b/MapControl/Shared/TilePyramidLayer.cs @@ -52,8 +52,6 @@ namespace MapControl private readonly Progress loadingProgress; private readonly UpdateTimer updateTimer; - private ITileImageLoader tileImageLoader; - private MapBase parentMap; protected TilePyramidLayer() { @@ -73,8 +71,8 @@ namespace MapControl public ITileImageLoader TileImageLoader { - get => tileImageLoader ??= new TileImageLoader(); - set => tileImageLoader = value; + get => field ??= new TileImageLoader(); + set; } /// @@ -152,26 +150,26 @@ namespace MapControl /// public MapBase ParentMap { - get => parentMap; + get; set { - if (parentMap != null) + if (field != null) { - parentMap.ViewportChanged -= OnViewportChanged; + field.ViewportChanged -= OnViewportChanged; } - parentMap = value; + field = value; - if (parentMap != null) + if (field != null) { - parentMap.ViewportChanged += OnViewportChanged; + field.ViewportChanged += OnViewportChanged; } updateTimer.Run(); } } - public bool IsBaseMapLayer => parentMap != null && parentMap.Children.Count > 0 && parentMap.Children[0] == this; + public bool IsBaseMapLayer => ParentMap != null && ParentMap.Children.Count > 0 && ParentMap.Children[0] == this; public abstract IReadOnlyCollection SupportedCrsIds { get; } diff --git a/MapControl/Shared/UriTileSource.cs b/MapControl/Shared/UriTileSource.cs index 1b638945..4d955bf6 100644 --- a/MapControl/Shared/UriTileSource.cs +++ b/MapControl/Shared/UriTileSource.cs @@ -4,22 +4,21 @@ namespace MapControl { public class UriTileSource : TileSource { - private string uriTemplate; private string uriFormat; public string UriTemplate { - get => uriTemplate; + get; set { - uriTemplate = value; - uriFormat = uriTemplate + field = value; + uriFormat = field .Replace("{z}", "{0}") .Replace("{x}", "{1}") .Replace("{y}", "{2}") .Replace("{s}", "{3}"); - if (Subdomains == null && uriTemplate.Contains("{s}")) + if (Subdomains == null && field.Contains("{s}")) { Subdomains = ["a", "b", "c"]; // default OpenStreetMap subdomains } diff --git a/MapControl/Shared/WmsImageLayer.cs b/MapControl/Shared/WmsImageLayer.cs index f66022bd..7f5076eb 100644 --- a/MapControl/Shared/WmsImageLayer.cs +++ b/MapControl/Shared/WmsImageLayer.cs @@ -27,8 +27,7 @@ namespace MapControl /// public class WmsImageLayer : MapImageLayer { - private static ILogger logger; - private static ILogger Logger => logger ??= ImageLoader.LoggerFactory?.CreateLogger(typeof(WmsImageLayer)); + private static ILogger Logger => field ??= ImageLoader.LoggerFactory?.CreateLogger(typeof(WmsImageLayer)); public static readonly DependencyProperty ServiceUriProperty = DependencyPropertyHelper.Register(nameof(ServiceUri), null, diff --git a/MapControl/Shared/WmtsTileLayer.cs b/MapControl/Shared/WmtsTileLayer.cs index 0ad56322..f953f2f1 100644 --- a/MapControl/Shared/WmtsTileLayer.cs +++ b/MapControl/Shared/WmtsTileLayer.cs @@ -28,8 +28,7 @@ namespace MapControl /// public class WmtsTileLayer : TilePyramidLayer { - private static ILogger logger; - private static ILogger Logger => logger ??= ImageLoader.LoggerFactory?.CreateLogger(typeof(WmtsTileLayer)); + private static ILogger Logger => field ??= ImageLoader.LoggerFactory?.CreateLogger(typeof(WmtsTileLayer)); public static readonly DependencyProperty CapabilitiesUriProperty = DependencyPropertyHelper.Register(nameof(CapabilitiesUri)); diff --git a/MapControl/WPF/MapControl.WPF.csproj b/MapControl/WPF/MapControl.WPF.csproj index fd24fe2a..557847a0 100644 --- a/MapControl/WPF/MapControl.WPF.csproj +++ b/MapControl/WPF/MapControl.WPF.csproj @@ -1,7 +1,7 @@  net10.0-windows;net462 - 12.0 + 14.0 true WPF MapControl diff --git a/MapControl/WPF/MapGraticule.WPF.cs b/MapControl/WPF/MapGraticule.WPF.cs index 059e1254..a511334b 100644 --- a/MapControl/WPF/MapGraticule.WPF.cs +++ b/MapControl/WPF/MapGraticule.WPF.cs @@ -18,26 +18,24 @@ namespace MapControl public static readonly DependencyProperty FontSizeProperty = TextElement.FontSizeProperty.AddOwner(typeof(MapGraticule), new FrameworkPropertyMetadata(12d)); - private MapBase parentMap; - /// /// Implements IMapElement.ParentMap. /// public MapBase ParentMap { - get => parentMap; + get; set { - if (parentMap != null) + if (field != null) { - parentMap.ViewportChanged -= OnViewportChanged; + field.ViewportChanged -= OnViewportChanged; } - parentMap = value; + field = value; - if (parentMap != null) + if (field != null) { - parentMap.ViewportChanged += OnViewportChanged; + field.ViewportChanged += OnViewportChanged; } } } @@ -49,7 +47,7 @@ namespace MapControl protected override void OnRender(DrawingContext drawingContext) { - if (parentMap != null) + if (ParentMap != null) { var pathGeometry = new PathGeometry(); diff --git a/MapProjections/Shared/GeoApiProjection.cs b/MapProjections/Shared/GeoApiProjection.cs index 1baf2ee2..060830b8 100644 --- a/MapProjections/Shared/GeoApiProjection.cs +++ b/MapProjections/Shared/GeoApiProjection.cs @@ -17,8 +17,6 @@ namespace MapControl.Projections /// public class GeoApiProjection : MapProjection { - private IProjectedCoordinateSystem coordinateSystem; - protected GeoApiProjection() { } @@ -44,23 +42,23 @@ namespace MapControl.Projections /// public IProjectedCoordinateSystem CoordinateSystem { - get => coordinateSystem; + get; protected set { - coordinateSystem = value ?? throw new ArgumentNullException(nameof(value)); + field = value ?? throw new ArgumentNullException(nameof(value)); var transformFactory = new CoordinateTransformationFactory(); LocationToMapTransform = transformFactory - .CreateFromCoordinateSystems(GeographicCoordinateSystem.WGS84, coordinateSystem) + .CreateFromCoordinateSystems(GeographicCoordinateSystem.WGS84, field) .MathTransform; MapToLocationTransform = transformFactory - .CreateFromCoordinateSystems(coordinateSystem, GeographicCoordinateSystem.WGS84) + .CreateFromCoordinateSystems(field, GeographicCoordinateSystem.WGS84) .MathTransform; - CrsId = !string.IsNullOrEmpty(coordinateSystem.Authority) && coordinateSystem.AuthorityCode > 0 - ? $"{coordinateSystem.Authority}:{coordinateSystem.AuthorityCode}" + CrsId = !string.IsNullOrEmpty(field.Authority) && field.AuthorityCode > 0 + ? $"{field.Authority}:{field.AuthorityCode}" : string.Empty; if (CrsId == "EPSG:3857") @@ -69,7 +67,7 @@ namespace MapControl.Projections } else { - var projection = coordinateSystem.Projection ?? + var projection = field.Projection ?? throw new ArgumentException("CoordinateSystem.Projection must not be null.", nameof(value)); var centralMeridian = projection.GetParameter("central_meridian") ?? projection.GetParameter("longitude_of_origin"); @@ -98,7 +96,7 @@ namespace MapControl.Projections public override Point GetRelativeScale(double latitude, double longitude) { - var k = coordinateSystem?.Projection?.GetParameter("scale_factor")?.Value ?? 1d; + var k = CoordinateSystem?.Projection?.GetParameter("scale_factor")?.Value ?? 1d; return new Point(k, k); } diff --git a/MapProjections/WPF/MapProjections.WPF.csproj b/MapProjections/WPF/MapProjections.WPF.csproj index 6f0a1e23..af398abf 100644 --- a/MapProjections/WPF/MapProjections.WPF.csproj +++ b/MapProjections/WPF/MapProjections.WPF.csproj @@ -1,7 +1,7 @@  net10.0-windows;net462 - 12.0 + 14.0 true WPF MapControl.Projections diff --git a/MapUiTools/WPF/MapUiTools.WPF.csproj b/MapUiTools/WPF/MapUiTools.WPF.csproj index 82c7538d..9df484fa 100644 --- a/MapUiTools/WPF/MapUiTools.WPF.csproj +++ b/MapUiTools/WPF/MapUiTools.WPF.csproj @@ -1,7 +1,7 @@  net10.0-windows;net462 - 12.0 + 14.0 true WPF MapControl.UiTools