From 020a871714029713fb5b06c187f7d666cac71b6b Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Fri, 13 Sep 2024 23:47:17 +0200 Subject: [PATCH] GeoImage and GroundOverlay implementation --- MapControl/Shared/GeoImage.cs | 4 +-- MapControl/Shared/GroundOverlay.cs | 21 +++++++++++----- MapControl/Shared/MapOverlaysPanel.cs | 31 ++++++++++++------------ MapUiTools/Shared/MapLayersMenuButton.cs | 23 +++++++++--------- 4 files changed, 45 insertions(+), 34 deletions(-) diff --git a/MapControl/Shared/GeoImage.cs b/MapControl/Shared/GeoImage.cs index ef382545..577bc2d9 100644 --- a/MapControl/Shared/GeoImage.cs +++ b/MapControl/Shared/GeoImage.cs @@ -74,11 +74,11 @@ namespace MapControl image.SetValue(SourcePathProperty, value); } - public static Image LoadGeoImage(string sourcePath) + public static async Task CreateAsync(string sourcePath) { var image = new Image(); - SetSourcePath(image, sourcePath); + await LoadGeoImageAsync(image, sourcePath); return image; } diff --git a/MapControl/Shared/GroundOverlay.cs b/MapControl/Shared/GroundOverlay.cs index 4d3bd023..243357b3 100644 --- a/MapControl/Shared/GroundOverlay.cs +++ b/MapControl/Shared/GroundOverlay.cs @@ -46,7 +46,7 @@ namespace MapControl public static readonly DependencyProperty SourcePathProperty = DependencyPropertyHelper.Register(nameof(SourcePath), null, - async (overlay, oldValue, newValue) => await overlay.SourcePathPropertyChanged(newValue)); + async (groundOverlay, oldValue, newValue) => await groundOverlay.LoadAsync(newValue)); public string SourcePath { @@ -54,7 +54,16 @@ namespace MapControl set => SetValue(SourcePathProperty, value); } - private async Task SourcePathPropertyChanged(string sourcePath) + public static async Task CreateAsync(string sourcePath) + { + var groundOverlay = new GroundOverlay(); + + await groundOverlay.LoadAsync(sourcePath); + + return groundOverlay; + } + + public async Task LoadAsync(string sourcePath) { IEnumerable imageOverlays = null; @@ -66,11 +75,11 @@ namespace MapControl if (ext == ".kmz") { - imageOverlays = await ReadGroundOverlaysFromArchiveAsync(sourcePath); + imageOverlays = await LoadGroundOverlaysFromArchiveAsync(sourcePath); } else if (ext == ".kml") { - imageOverlays = await ReadGroundOverlaysFromFileAsync(sourcePath); + imageOverlays = await LoadGroundOverlaysFromFileAsync(sourcePath); } } catch (Exception ex) @@ -98,7 +107,7 @@ namespace MapControl } } - private static async Task> ReadGroundOverlaysFromArchiveAsync(string archiveFilePath) + private static async Task> LoadGroundOverlaysFromArchiveAsync(string archiveFilePath) { using (var archive = ZipFile.OpenRead(archiveFilePath)) { @@ -141,7 +150,7 @@ namespace MapControl } } - private static async Task> ReadGroundOverlaysFromFileAsync(string docFilePath) + private static async Task> LoadGroundOverlaysFromFileAsync(string docFilePath) { docFilePath = FilePath.GetFullPath(docFilePath); diff --git a/MapControl/Shared/MapOverlaysPanel.cs b/MapControl/Shared/MapOverlaysPanel.cs index db888220..ed9e778c 100644 --- a/MapControl/Shared/MapOverlaysPanel.cs +++ b/MapControl/Shared/MapOverlaysPanel.cs @@ -8,6 +8,7 @@ using System.Collections.Specialized; using System.Diagnostics; using System.IO; using System.Linq; +using System.Threading.Tasks; #if WPF using System.Windows; #elif UWP @@ -25,7 +26,7 @@ namespace MapControl { public static readonly DependencyProperty SourcePathsProperty = DependencyPropertyHelper.Register>(nameof(SourcePaths), null, - (control, oldValue, newValue) => control.SourcePathsPropertyChanged(oldValue, newValue)); + async (control, oldValue, newValue) => await control.SourcePathsPropertyChanged(oldValue, newValue)); public IEnumerable SourcePaths { @@ -33,7 +34,7 @@ namespace MapControl set => SetValue(SourcePathsProperty, value); } - private void SourcePathsPropertyChanged(IEnumerable oldSourcePaths, IEnumerable newSourcePaths) + private async Task SourcePathsPropertyChanged(IEnumerable oldSourcePaths, IEnumerable newSourcePaths) { Children.Clear(); @@ -49,16 +50,16 @@ namespace MapControl newCollection.CollectionChanged += SourcePathsCollectionChanged; } - AddOverlays(0, newSourcePaths); + await AddOverlays(0, newSourcePaths); } } - private void SourcePathsCollectionChanged(object sender, NotifyCollectionChangedEventArgs args) + private async void SourcePathsCollectionChanged(object sender, NotifyCollectionChangedEventArgs args) { switch (args.Action) { case NotifyCollectionChangedAction.Add: - AddOverlays(args.NewStartingIndex, args.NewItems.Cast()); + await AddOverlays(args.NewStartingIndex, args.NewItems.Cast()); break; case NotifyCollectionChangedAction.Remove: @@ -67,33 +68,33 @@ namespace MapControl case NotifyCollectionChangedAction.Move: RemoveOverlays(args.OldStartingIndex, args.OldItems.Count); - AddOverlays(args.NewStartingIndex, args.NewItems.Cast()); + await AddOverlays(args.NewStartingIndex, args.NewItems.Cast()); break; case NotifyCollectionChangedAction.Replace: - ReplaceOverlays(args.NewStartingIndex, args.NewItems.Cast()); + await ReplaceOverlays(args.NewStartingIndex, args.NewItems.Cast()); break; case NotifyCollectionChangedAction.Reset: Children.Clear(); - AddOverlays(0, SourcePaths); + await AddOverlays(0, SourcePaths); break; } } - private void AddOverlays(int index, IEnumerable sourcePaths) + private async Task AddOverlays(int index, IEnumerable sourcePaths) { foreach (var sourcePath in sourcePaths) { - Children.Insert(index++, CreateOverlay(sourcePath)); + Children.Insert(index++, await CreateOverlay(sourcePath)); } } - private void ReplaceOverlays(int index, IEnumerable sourcePaths) + private async Task ReplaceOverlays(int index, IEnumerable sourcePaths) { foreach (var sourcePath in sourcePaths) { - Children[index++] = CreateOverlay(sourcePath); + Children[index++] = await CreateOverlay(sourcePath); } } @@ -105,7 +106,7 @@ namespace MapControl } } - protected virtual FrameworkElement CreateOverlay(string sourcePath) + protected virtual async Task CreateOverlay(string sourcePath) { FrameworkElement overlay; var ext = Path.GetExtension(sourcePath).ToLower(); @@ -114,11 +115,11 @@ namespace MapControl { if (ext == ".kmz" || ext == ".kml") { - overlay = new GroundOverlay { SourcePath = sourcePath }; + overlay = await GroundOverlay.CreateAsync(sourcePath); } else { - overlay = GeoImage.LoadGeoImage(sourcePath); + overlay = await GeoImage.CreateAsync(sourcePath); } } catch (Exception ex) diff --git a/MapUiTools/Shared/MapLayersMenuButton.cs b/MapUiTools/Shared/MapLayersMenuButton.cs index c6644045..ceac9b2c 100644 --- a/MapUiTools/Shared/MapLayersMenuButton.cs +++ b/MapUiTools/Shared/MapLayersMenuButton.cs @@ -6,6 +6,7 @@ using System; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Linq; +using System.Threading.Tasks; #if WPF using System.Windows; using System.Windows.Markup; @@ -38,9 +39,9 @@ namespace MapControl.UiTools public string Text { get; set; } - public Func LayerFactory { get; set; } + public Func> LayerFactory { get; set; } - public FrameworkElement GetLayer() => Layer ?? (Layer = LayerFactory?.Invoke()); + public async Task GetLayer() => Layer ?? (Layer = await LayerFactory?.Invoke()); } #if WPF @@ -55,13 +56,13 @@ namespace MapControl.UiTools public MapLayersMenuButton() : base("\uE81E") { - ((INotifyCollectionChanged)MapLayers).CollectionChanged += (s, e) => InitializeMenu(); - ((INotifyCollectionChanged)MapOverlays).CollectionChanged += (s, e) => InitializeMenu(); + ((INotifyCollectionChanged)MapLayers).CollectionChanged += async (s, e) => await InitializeMenu(); + ((INotifyCollectionChanged)MapOverlays).CollectionChanged += async (s, e) => await InitializeMenu(); } public static readonly DependencyProperty MapProperty = DependencyPropertyHelper.Register(nameof(Map), null, - (button, oldValue, newValue) => button.InitializeMenu()); + async (button, oldValue, newValue) => await button.InitializeMenu()); public MapBase Map { @@ -76,7 +77,7 @@ namespace MapControl.UiTools public Collection MapOverlays { get; } = new ObservableCollection(); - private void InitializeMenu() + private async Task InitializeMenu() { if (Map != null) { @@ -104,25 +105,25 @@ namespace MapControl.UiTools if (initialLayer != null) { - SetMapLayer(initialLayer); + SetMapLayer(await initialLayer); } } } - private void MapLayerClicked(object sender, RoutedEventArgs e) + private async void MapLayerClicked(object sender, RoutedEventArgs e) { var item = (FrameworkElement)sender; var mapLayerItem = (MapLayerItem)item.Tag; - SetMapLayer(mapLayerItem.GetLayer()); + SetMapLayer(await mapLayerItem.GetLayer()); } - private void MapOverlayClicked(object sender, RoutedEventArgs e) + private async void MapOverlayClicked(object sender, RoutedEventArgs e) { var item = (FrameworkElement)sender; var mapLayerItem = (MapLayerItem)item.Tag; - ToggleMapOverlay(mapLayerItem.GetLayer()); + ToggleMapOverlay(await mapLayerItem.GetLayer()); } private void SetMapLayer(FrameworkElement layer)