From 1d1b2942b432eba73a233b00f4a71e564dc7ffd0 Mon Sep 17 00:00:00 2001 From: Clemens Date: Thu, 13 Jan 2022 20:12:49 +0100 Subject: [PATCH] Reworked sample applications --- MapUiTools/Shared/MapLayersMenuButton.cs | 60 +++--- MapUiTools/Shared/MapProjectionsMenuButton.cs | 41 ++-- SampleApps/Shared/MapViewModel.cs | 175 ------------------ SampleApps/UniversalApp/MainPage.xaml | 94 +++++++++- SampleApps/UniversalApp/MainPage.xaml.cs | 62 +++++-- SampleApps/WinUiApp/MainWindow.xaml | 94 +++++++++- SampleApps/WinUiApp/MainWindow.xaml.cs | 72 +++++-- SampleApps/WpfApplication/MainWindow.xaml | 80 +++++++- SampleApps/WpfApplication/MainWindow.xaml.cs | 73 ++++++-- 9 files changed, 468 insertions(+), 283 deletions(-) diff --git a/MapUiTools/Shared/MapLayersMenuButton.cs b/MapUiTools/Shared/MapLayersMenuButton.cs index d1bf671e..c3d09571 100644 --- a/MapUiTools/Shared/MapLayersMenuButton.cs +++ b/MapUiTools/Shared/MapLayersMenuButton.cs @@ -2,78 +2,84 @@ // © 2022 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) -using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Collections.Specialized; using System.Linq; #if WINUI using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Markup; #elif UWP using Windows.UI.Xaml; +using Windows.UI.Xaml.Markup; #else using System.Windows; +using System.Windows.Markup; #endif namespace MapControl.UiTools { +#if WINUI || UWP + [ContentProperty(Name = nameof(Layer))] +#else + [ContentProperty(nameof(Layer))] +#endif + public class MapLayerItem + { + public string Text { get; set; } + public UIElement Layer { get; set; } + } + +#if WINUI || UWP + [ContentProperty(Name = nameof(MapLayers))] +#else + [ContentProperty(nameof(MapLayers))] +#endif public class MapLayersMenuButton : MenuButton { public MapLayersMenuButton() : base("\uE81E") { + ((INotifyCollectionChanged)MapLayers).CollectionChanged += (s, e) => InitializeMenu(); + ((INotifyCollectionChanged)MapOverlays).CollectionChanged += (s, e) => InitializeMenu(); } public static readonly DependencyProperty MapProperty = DependencyProperty.Register( nameof(Map), typeof(MapBase), typeof(MapLayersMenuButton), new PropertyMetadata(null, (o, e) => ((MapLayersMenuButton)o).InitializeMenu())); - public static readonly DependencyProperty MapLayersProperty = DependencyProperty.Register( - nameof(MapLayers), typeof(IDictionary), typeof(MapLayersMenuButton), - new PropertyMetadata(null, (o, e) => ((MapLayersMenuButton)o).InitializeMenu())); - - public static readonly DependencyProperty MapOverlaysProperty = DependencyProperty.Register( - nameof(MapOverlays), typeof(IDictionary), typeof(MapLayersMenuButton), - new PropertyMetadata(null, (o, e) => ((MapLayersMenuButton)o).InitializeMenu())); - public MapBase Map { get { return (MapBase)GetValue(MapProperty); } set { SetValue(MapProperty, value); } } - public IDictionary MapLayers - { - get { return (IDictionary)GetValue(MapLayersProperty); } - set { SetValue(MapLayersProperty, value); } - } + public Collection MapLayers { get; } = new ObservableCollection(); - public IDictionary MapOverlays - { - get { return (IDictionary)GetValue(MapOverlaysProperty); } - set { SetValue(MapOverlaysProperty, value); } - } + public Collection MapOverlays { get; } = new ObservableCollection(); private void InitializeMenu() { - if (Map != null && MapLayers != null) + if (Map != null) { var menu = CreateMenu(); - foreach (var layer in MapLayers) + foreach (var item in MapLayers) { - menu.Items.Add(CreateMenuItem(layer.Key, layer.Value, MapLayerClicked)); + menu.Items.Add(CreateMenuItem(item.Text, item.Layer, MapLayerClicked)); } - var initialLayer = MapLayers.Values.FirstOrDefault(); + var initialLayer = MapLayers.Select(l => l.Layer).FirstOrDefault(); - if (MapOverlays != null && MapOverlays.Any()) + if (MapOverlays.Count > 0) { if (initialLayer != null) { menu.Items.Add(CreateSeparator()); } - foreach (var overlay in MapOverlays) + foreach (var item in MapOverlays) { - menu.Items.Add(CreateMenuItem(overlay.Key, overlay.Value, MapOverlayClicked)); + menu.Items.Add(CreateMenuItem(item.Text, item.Layer, MapOverlayClicked)); } } @@ -117,7 +123,7 @@ namespace MapControl.UiTools { int index = 1; - foreach (var overlay in MapOverlays.Values) + foreach (var overlay in MapOverlays.Select(l => l.Layer)) { if (overlay == layer) { diff --git a/MapUiTools/Shared/MapProjectionsMenuButton.cs b/MapUiTools/Shared/MapProjectionsMenuButton.cs index a5359298..30fa9572 100644 --- a/MapUiTools/Shared/MapProjectionsMenuButton.cs +++ b/MapUiTools/Shared/MapProjectionsMenuButton.cs @@ -2,57 +2,70 @@ // © 2022 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) -using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Collections.Specialized; using System.Linq; #if WINUI using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Markup; #elif UWP using Windows.UI.Xaml; +using Windows.UI.Xaml.Markup; #else using System.Windows; +using System.Windows.Markup; #endif namespace MapControl.UiTools { +#if WINUI || UWP + [ContentProperty(Name = nameof(Projection))] +#else + [ContentProperty(nameof(Projection))] +#endif + public class MapProjectionItem + { + public string Text { get; set; } + public MapProjection Projection { get; set; } + } + +#if WINUI || UWP + [ContentProperty(Name = nameof(MapProjections))] +#else + [ContentProperty(nameof(MapProjections))] +#endif public class MapProjectionsMenuButton : MenuButton { public MapProjectionsMenuButton() : base("\uE809") { + ((INotifyCollectionChanged)MapProjections).CollectionChanged += (s, e) => InitializeMenu(); } public static readonly DependencyProperty MapProperty = DependencyProperty.Register( nameof(Map), typeof(MapBase), typeof(MapProjectionsMenuButton), new PropertyMetadata(null, (o, e) => ((MapProjectionsMenuButton)o).InitializeMenu())); - public static readonly DependencyProperty MapProjectionsProperty = DependencyProperty.Register( - nameof(MapProjections), typeof(IDictionary), typeof(MapProjectionsMenuButton), - new PropertyMetadata(null, (o, e) => ((MapProjectionsMenuButton)o).InitializeMenu())); - public MapBase Map { get { return (MapBase)GetValue(MapProperty); } set { SetValue(MapProperty, value); } } - public IDictionary MapProjections - { - get { return (IDictionary)GetValue(MapProjectionsProperty); } - set { SetValue(MapProjectionsProperty, value); } - } + public Collection MapProjections { get; } = new ObservableCollection(); private void InitializeMenu() { - if (Map != null && MapProjections != null) + if (Map != null) { var menu = CreateMenu(); - foreach (var projection in MapProjections) + foreach (var item in MapProjections) { - menu.Items.Add(CreateMenuItem(projection.Key, projection.Value, MapProjectionClicked)); + menu.Items.Add(CreateMenuItem(item.Text, item.Projection, MapProjectionClicked)); } - var initialProjection = MapProjections.Values.FirstOrDefault(); + var initialProjection = MapProjections.Select(p => p.Projection).FirstOrDefault(); if (initialProjection != null) { diff --git a/SampleApps/Shared/MapViewModel.cs b/SampleApps/Shared/MapViewModel.cs index 27a51042..9ff00ab3 100644 --- a/SampleApps/Shared/MapViewModel.cs +++ b/SampleApps/Shared/MapViewModel.cs @@ -1,24 +1,5 @@ using MapControl; -using System; using System.Collections.Generic; -#if WINUI -using Microsoft.UI; -using Microsoft.UI.Xaml; -using Microsoft.UI.Xaml.Controls; -using Microsoft.UI.Xaml.Media; -using Microsoft.UI.Xaml.Media.Imaging; -#elif UWP -using Windows.UI; -using Windows.UI.Xaml; -using Windows.UI.Xaml.Controls; -using Windows.UI.Xaml.Media; -using Windows.UI.Xaml.Media.Imaging; -#else -using System.Windows; -using System.Windows.Controls; -using System.Windows.Media; -using System.Windows.Media.Imaging; -#endif namespace SampleApplication { @@ -40,164 +21,8 @@ namespace SampleApplication public List Pushpins { get; } = new List(); public List Polylines { get; } = new List(); - public Dictionary MapProjections { get; } = new Dictionary - { - { "Web Mercator", new WebMercatorProjection() }, - { "World Mercator", new WorldMercatorProjection() }, - { "Equirectangular", new EquirectangularProjection() }, - { "Orthographic", new OrthographicProjection() }, - { "Gnomonic", new GnomonicProjection() }, - { "Stereographic", new StereographicProjection() } - }; - - public Dictionary MapLayers { get; } = new Dictionary - { - { - "OpenStreetMap", - new MapTileLayer - { - TileSource = new TileSource { UriFormat = "https://tile.openstreetmap.org/{z}/{x}/{y}.png" }, - SourceName = "OpenStreetMap", - Description = "© [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)" - } - }, - { - "OpenStreetMap German", - new MapTileLayer - { - TileSource = new TileSource { UriFormat = "https://{s}.tile.openstreetmap.de/{z}/{x}/{y}.png" }, - SourceName = "OpenStreetMap German", - Description = "© [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)" - } - }, - { - "OpenStreetMap French", - new MapTileLayer - { - TileSource = new TileSource { UriFormat = "http://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png" }, - SourceName = "OpenStreetMap French", - Description = "© [OpenStreetMap France](https://www.openstreetmap.fr/mentions-legales/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)" - } - }, - { - "OpenTopoMap", - new MapTileLayer - { - TileSource = new TileSource { UriFormat = "https://tile.opentopomap.org/{z}/{x}/{y}.png" }, - SourceName = "OpenTopoMap", - Description = "© [OpenTopoMap](https://opentopomap.org/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)", - MaxZoomLevel = 17 - } - }, - { - "TopPlusOpen WMTS", - new WmtsTileLayer - { - SourceName = "TopPlusOpen", - Description = "© [BKG](https://gdz.bkg.bund.de/index.php/default/webdienste/topplus-produkte/wmts-topplusopen-wmts-topplus-open.html)", - CapabilitiesUri = new Uri("https://sgx.geodatenzentrum.de/wmts_topplus_open/1.0.0/WMTSCapabilities.xml") - } - }, - { - "TopPlusOpen WMS", - new WmsImageLayer - { - Description = "© [BKG](https://gdz.bkg.bund.de/index.php/default/webdienste/topplus-produkte/wms-topplusopen-mit-layer-fur-normalausgabe-und-druck-wms-topplus-open.html)", - ServiceUri = new Uri("https://sgx.geodatenzentrum.de/wms_topplus_open") - } - }, - { - "OpenStreetMap WMS", - new WmsImageLayer - { - Description = "© [terrestris GmbH & Co. KG](http://ows.terrestris.de/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)", - ServiceUri = new Uri("http://ows.terrestris.de/osm/service") - } - } - }; - - public Dictionary MapOverlays { get; } = new Dictionary - { - { - "Sample Image", - new Image() - }, - { - "Seamarks", - new MapTileLayer - { - TileSource = new TileSource { UriFormat = "http://tiles.openseamap.org/seamark/{z}/{x}/{y}.png" }, - SourceName = "OpenSeaMap", - MinZoomLevel = 9, - MaxZoomLevel = 18 - } - }, - { - "Graticule", - new MapGraticule - { - Opacity = 0.75 - } - }, - { - "Scale", - new MapScale - { - HorizontalAlignment = HorizontalAlignment.Center, - VerticalAlignment = VerticalAlignment.Bottom - } - } - }; - public MapViewModel() { - // Add Bing Maps TileLayers with tile URLs retrieved from the Imagery Metadata Service - // (http://msdn.microsoft.com/en-us/library/ff701716.aspx). - // A Bing Maps API Key (http://msdn.microsoft.com/en-us/library/ff428642.aspx) is required - // for using these layers and must be assigned to the static BingMapsTileLayer.ApiKey property. - - if (!string.IsNullOrEmpty(BingMapsTileLayer.ApiKey)) - { - MapLayers.Add( - "Bing Maps Road", - new BingMapsTileLayer - { - Mode = BingMapsTileLayer.MapMode.Road, - SourceName = "Bing Maps Road", - Description = "© [Microsoft](http://www.bing.com/maps/)" - }); - - MapLayers.Add( - "Bing Maps Aerial", - new BingMapsTileLayer - { - Mode = BingMapsTileLayer.MapMode.Aerial, - SourceName = "Bing Maps Aerial", - Description = "© [Microsoft](http://www.bing.com/maps/)", - MapForeground = new SolidColorBrush(Colors.White), - MapBackground = new SolidColorBrush(Colors.Black) - }); - - MapLayers.Add( - "Bing Maps Aerial with Labels", - new BingMapsTileLayer - { - Mode = BingMapsTileLayer.MapMode.AerialWithLabels, - SourceName = "Bing Maps Hybrid", - Description = "© [Microsoft](http://www.bing.com/maps/)", - MapForeground = new SolidColorBrush(Colors.White), - MapBackground = new SolidColorBrush(Colors.Black) - }); - } - - var sampleImage = (Image)MapOverlays["Sample Image"]; -#if WINUI || UWP - sampleImage.Source = new BitmapImage(new Uri("ms-appx:///10_535_330.jpg")); -#else - sampleImage.Source = new BitmapImage(new Uri("pack://siteoforigin:,,,/10_535_330.jpg")); -#endif - MapPanel.SetBoundingBox(sampleImage, new BoundingBox(53.54031, 8.08594, 53.74871, 8.43750)); - Points.Add(new PointItem { Name = "Steinbake Leitdamm", diff --git a/SampleApps/UniversalApp/MainPage.xaml b/SampleApps/UniversalApp/MainPage.xaml index f22c0329..d1d3f519 100644 --- a/SampleApps/UniversalApp/MainPage.xaml +++ b/SampleApps/UniversalApp/MainPage.xaml @@ -86,7 +86,6 @@ @@ -116,16 +115,95 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Map="{Binding ElementName=map}"> + + + + + + + @@ -117,16 +116,95 @@ - + Map="{Binding ElementName=map}"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + Map="{Binding ElementName=map}"> + + + + + + + - + - + Map="{Binding ElementName=map}"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + Map="{Binding ElementName=map}"> + + + + + + + @@ -48,6 +87,8 @@ namespace SampleApplication } } + partial void AddChartServerLayer(); + private void ResetHeadingButtonClick(object sender, RoutedEventArgs e) { map.TargetHeading = 0d;