diff --git a/MapControl/Shared/MapItemsControl.cs b/MapControl/Shared/MapItemsControl.cs index 16c469bf..d411f182 100644 --- a/MapControl/Shared/MapItemsControl.cs +++ b/MapControl/Shared/MapItemsControl.cs @@ -26,13 +26,6 @@ namespace MapControl new PropertyMetadata(null, (o, e) => BindingOperations.SetBinding( o, LocationProperty, new Binding { Path = new PropertyPath((string)e.NewValue) }))); - public MapItem() - { - DefaultStyleKey = typeof(MapItem); - - MapPanel.InitMapElement(this); - } - /// /// Gets/sets MapPanel.AutoCollapse. /// @@ -66,13 +59,6 @@ namespace MapControl /// public partial class MapItemsControl : ListBox { - public MapItemsControl() - { - DefaultStyleKey = typeof(MapItemsControl); - - MapPanel.InitMapElement(this); - } - protected override DependencyObject GetContainerForItemOverride() { return new MapItem(); diff --git a/MapControl/Shared/MapPanel.cs b/MapControl/Shared/MapPanel.cs index 63ec8ef1..72e62a04 100644 --- a/MapControl/Shared/MapPanel.cs +++ b/MapControl/Shared/MapPanel.cs @@ -42,11 +42,6 @@ namespace MapControl set { SetParentMap(value); } } - public MapPanel() - { - InitMapElement(this); - } - /// /// Gets a value that controls whether an element's Visibility is automatically /// set to Collapsed when it is located outside the visible viewport area. diff --git a/MapControl/Shared/MapPath.cs b/MapControl/Shared/MapPath.cs index d412455a..87cd5a17 100644 --- a/MapControl/Shared/MapPath.cs +++ b/MapControl/Shared/MapPath.cs @@ -24,11 +24,6 @@ namespace MapControl private MapBase parentMap; - public MapPath() - { - MapPanel.InitMapElement(this); - } - /// /// Gets or sets a Location that is used as /// - either the origin point of a geometry specified in cartesian map units (meters) diff --git a/MapControl/Shared/MapTileLayerBase.cs b/MapControl/Shared/MapTileLayerBase.cs index d9e6ba99..94cb909e 100644 --- a/MapControl/Shared/MapTileLayerBase.cs +++ b/MapControl/Shared/MapTileLayerBase.cs @@ -61,7 +61,9 @@ namespace MapControl updateTimer = new DispatcherTimer { Interval = UpdateInterval }; updateTimer.Tick += (s, e) => Update(false); +#if WINDOWS_UWP MapPanel.InitMapElement(this); +#endif } public ITileImageLoader TileImageLoader { get; } diff --git a/MapControl/UWP/MapControl.UWP.csproj b/MapControl/UWP/MapControl.UWP.csproj index 6f0d63d8..056346d9 100644 --- a/MapControl/UWP/MapControl.UWP.csproj +++ b/MapControl/UWP/MapControl.UWP.csproj @@ -131,9 +131,6 @@ OrthographicProjection.cs - - Pushpin.cs - StereographicProjection.cs @@ -198,6 +195,7 @@ + diff --git a/MapControl/UWP/MapItemsControl.UWP.cs b/MapControl/UWP/MapItemsControl.UWP.cs index 1efb1af1..5837cb39 100644 --- a/MapControl/UWP/MapItemsControl.UWP.cs +++ b/MapControl/UWP/MapItemsControl.UWP.cs @@ -19,6 +19,12 @@ namespace MapControl nameof(Location), typeof(Location), typeof(MapItem), new PropertyMetadata(null, (o, e) => MapPanel.SetLocation((MapItem)o, (Location)e.NewValue))); + public MapItem() + { + DefaultStyleKey = typeof(MapItem); + MapPanel.InitMapElement(this); + } + protected override void OnPointerPressed(PointerRoutedEventArgs e) { (ItemsControl.ItemsControlFromItemContainer(this) as MapItemsControl)?.OnItemClicked( @@ -28,6 +34,12 @@ namespace MapControl public partial class MapItemsControl { + public MapItemsControl() + { + DefaultStyleKey = typeof(MapItemsControl); + MapPanel.InitMapElement(this); + } + public new FrameworkElement ContainerFromItem(object item) { return (FrameworkElement)base.ContainerFromItem(item); diff --git a/MapControl/UWP/MapPanel.UWP.cs b/MapControl/UWP/MapPanel.UWP.cs index b91efeab..a2382e3e 100644 --- a/MapControl/UWP/MapPanel.UWP.cs +++ b/MapControl/UWP/MapPanel.UWP.cs @@ -23,6 +23,11 @@ namespace MapControl private static readonly DependencyProperty ViewPositionProperty = DependencyProperty.RegisterAttached( "ViewPosition", typeof(Point?), typeof(MapPanel), new PropertyMetadata(null)); + public MapPanel() + { + InitMapElement(this); + } + public static void InitMapElement(FrameworkElement element) { if (element is MapBase) @@ -31,7 +36,7 @@ namespace MapControl } else { - // Workaround for missing property value inheritance in Windows Runtime. + // Workaround for missing property value inheritance in UWP. // Loaded and Unloaded handlers set and clear the ParentMap property value. element.Loaded += (s, e) => GetParentMap(element); diff --git a/MapControl/UWP/MapPath.UWP.cs b/MapControl/UWP/MapPath.UWP.cs index c54d73a2..285d9a36 100644 --- a/MapControl/UWP/MapPath.UWP.cs +++ b/MapControl/UWP/MapPath.UWP.cs @@ -14,6 +14,11 @@ namespace MapControl { public partial class MapPath : Path { + public MapPath() + { + MapPanel.InitMapElement(this); + } + #region Methods used only by derived classes MapPolyline and MapPolygon protected void DataCollectionPropertyChanged(DependencyPropertyChangedEventArgs e) diff --git a/MapControl/Shared/Pushpin.cs b/MapControl/UWP/Pushpin.cs similarity index 81% rename from MapControl/Shared/Pushpin.cs rename to MapControl/UWP/Pushpin.cs index b073f2f3..d89df02a 100644 --- a/MapControl/Shared/Pushpin.cs +++ b/MapControl/UWP/Pushpin.cs @@ -2,13 +2,8 @@ // © 2021 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) -#if WINDOWS_UWP using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; -#else -using System.Windows; -using System.Windows.Controls; -#endif namespace MapControl { @@ -17,7 +12,6 @@ namespace MapControl /// public class Pushpin : ContentControl { -#if WINDOWS_UWP public static readonly DependencyProperty AutoCollapseProperty = DependencyProperty.Register( nameof(AutoCollapse), typeof(bool), typeof(Pushpin), new PropertyMetadata(false, (o, e) => MapPanel.SetAutoCollapse((Pushpin)o, (bool)e.NewValue))); @@ -25,15 +19,10 @@ namespace MapControl public static readonly DependencyProperty LocationProperty = DependencyProperty.Register( nameof(Location), typeof(Location), typeof(Pushpin), new PropertyMetadata(null, (o, e) => MapPanel.SetLocation((Pushpin)o, (Location)e.NewValue))); -#else - public static readonly DependencyProperty AutoCollapseProperty = MapPanel.AutoCollapseProperty.AddOwner(typeof(Pushpin)); - public static readonly DependencyProperty LocationProperty = MapPanel.LocationProperty.AddOwner(typeof(Pushpin)); -#endif public Pushpin() { DefaultStyleKey = typeof(Pushpin); - MapPanel.InitMapElement(this); } diff --git a/MapControl/WPF/MapItemsControl.WPF.cs b/MapControl/WPF/MapItemsControl.WPF.cs index 8c42633c..713bcce3 100644 --- a/MapControl/WPF/MapItemsControl.WPF.cs +++ b/MapControl/WPF/MapItemsControl.WPF.cs @@ -15,6 +15,11 @@ namespace MapControl public static readonly DependencyProperty LocationProperty = MapPanel.LocationProperty.AddOwner(typeof(MapItem)); + static MapItem() + { + DefaultStyleKeyProperty.OverrideMetadata(typeof(MapItem), new FrameworkPropertyMetadata(typeof(MapItem))); + } + protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { (ItemsControl.ItemsControlFromItemContainer(this) as MapItemsControl)?.OnItemClicked( @@ -24,6 +29,11 @@ namespace MapControl public partial class MapItemsControl { + static MapItemsControl() + { + DefaultStyleKeyProperty.OverrideMetadata(typeof(MapItemsControl), new FrameworkPropertyMetadata(typeof(MapItemsControl))); + } + public FrameworkElement ContainerFromItem(object item) { return (FrameworkElement)ItemContainerGenerator.ContainerFromItem(item); diff --git a/MapControl/WPF/MapPanel.WPF.cs b/MapControl/WPF/MapPanel.WPF.cs index 12709f04..82db317d 100644 --- a/MapControl/WPF/MapPanel.WPF.cs +++ b/MapControl/WPF/MapPanel.WPF.cs @@ -26,19 +26,19 @@ namespace MapControl public static readonly DependencyProperty ParentMapProperty = ParentMapPropertyKey.DependencyProperty; public static readonly DependencyProperty ViewPositionProperty = ViewPositionPropertyKey.DependencyProperty; + public MapPanel() + { + if (this is MapBase) + { + SetValue(ParentMapPropertyKey, this); + } + } + public static MapBase GetParentMap(FrameworkElement element) { return (MapBase)element.GetValue(ParentMapProperty); } - public static void InitMapElement(FrameworkElement element) - { - if (element is MapBase) - { - element.SetValue(ParentMapPropertyKey, element); - } - } - private static void SetViewPosition(FrameworkElement element, Point? viewPosition) { element.SetValue(ViewPositionPropertyKey, viewPosition); diff --git a/MapControl/WPF/Pushpin.cs b/MapControl/WPF/Pushpin.cs new file mode 100644 index 00000000..1a40a8b4 --- /dev/null +++ b/MapControl/WPF/Pushpin.cs @@ -0,0 +1,42 @@ +// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control +// © 2021 Clemens Fischer +// Licensed under the Microsoft Public License (Ms-PL) + +using System.Windows; +using System.Windows.Controls; + +namespace MapControl +{ + /// + /// Pushpin at a geographic location specified by the Location property. + /// + public class Pushpin : ContentControl + { + public static readonly DependencyProperty AutoCollapseProperty = MapPanel.AutoCollapseProperty.AddOwner(typeof(Pushpin)); + + public static readonly DependencyProperty LocationProperty = MapPanel.LocationProperty.AddOwner(typeof(Pushpin)); + + static Pushpin() + { + DefaultStyleKeyProperty.OverrideMetadata(typeof(Pushpin), new FrameworkPropertyMetadata(typeof(Pushpin))); + } + + /// + /// Gets/sets MapPanel.AutoCollapse. + /// + public bool AutoCollapse + { + get { return (bool)GetValue(AutoCollapseProperty); } + set { SetValue(AutoCollapseProperty, value); } + } + + /// + /// Gets/sets MapPanel.Location. + /// + public Location Location + { + get { return (Location)GetValue(LocationProperty); } + set { SetValue(LocationProperty, value); } + } + } +}