forked from Esri/arcgis-maps-sdk-dotnet-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestoreAutoPanMode.cs
More file actions
129 lines (113 loc) · 3.29 KB
/
RestoreAutoPanMode.cs
File metadata and controls
129 lines (113 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Location;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
#if NETFX_CORE
using Windows.UI.Xaml;
#else
using System.Windows.Threading;
#endif
namespace RoutingSample
{
public class RestoreAutoPanMode
{
private class DelayTimer
{
private Action m_action;
DispatcherTimer m_timer;
public DelayTimer(Action action)
{
m_timer = new DispatcherTimer();
m_timer.Tick += m_timer_Tick;
m_action = action;
}
#if NETFX_CORE
void m_timer_Tick(object sender, object e)
#else
void m_timer_Tick(object sender, EventArgs e)
#endif
{
m_timer.Stop();
if (m_action != null)
m_action();
}
public void Invoke(TimeSpan delay)
{
m_timer.Stop();
m_timer.Interval = delay;
m_timer.Start();
}
public void Cancel()
{
m_timer.Stop();
}
}
private MapView m_mapView;
private DelayTimer m_timer;
public RestoreAutoPanMode()
{
m_timer = new DelayTimer(ResetPanMode);
}
private void ResetPanMode()
{
if (m_mapView != null && m_mapView.LocationDisplay != null)
m_mapView.LocationDisplay.AutoPanMode = this.PanMode;
}
internal void AttachToMapView(MapView mv)
{
if (m_mapView != null && m_mapView != mv)
throw new InvalidOperationException("RestoreAutoPanMode can only be assigned to one mapview");
m_mapView = mv;
m_mapView.PropertyChanged += m_mapView_PropertyChanged;
}
internal void DetachFromMapView(MapView mv)
{
m_mapView.PropertyChanged -= m_mapView_PropertyChanged;
m_mapView = null;
}
private void m_mapView_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
//If user stopped navigating and we're not in the correct autopan mode,
//restore autopan after the set delay.
if(IsEnabled && e.PropertyName == "IsNavigating")
{
if(m_mapView.LocationDisplay != null &&
m_mapView.LocationDisplay.AutoPanMode != PanMode)
{
if (!m_mapView.IsNavigating)
m_timer.Invoke(TimeSpan.FromSeconds(DelayInSeconds));
else
m_timer.Cancel();
}
}
}
public bool IsEnabled { get; set; }
public int DelayInSeconds { get; set; }
public AutoPanMode PanMode { get; set; }
public static RestoreAutoPanMode GetRestoreAutoPanSettings(DependencyObject obj)
{
return (RestoreAutoPanMode)obj.GetValue(RestoreAutoPanSettingsProperty);
}
public static void SetRestoreAutoPanSettings(DependencyObject obj, RestoreAutoPanMode value)
{
obj.SetValue(RestoreAutoPanSettingsProperty, value);
}
public static readonly DependencyProperty RestoreAutoPanSettingsProperty =
DependencyProperty.RegisterAttached("RestoreAutoPanSettings", typeof(RestoreAutoPanMode), typeof(RestoreAutoPanMode),
new PropertyMetadata(null, OnRestoreAutoPanSettingsChanged));
private static void OnRestoreAutoPanSettingsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (!(d is MapView))
throw new InvalidOperationException("This property must be attached to a mapview");
MapView mv = (MapView)d;
var oldValue = e.OldValue as RestoreAutoPanMode;
if (oldValue != null)
oldValue.DetachFromMapView(mv);
var newValue = e.NewValue as RestoreAutoPanMode;
if (newValue != null)
newValue.AttachToMapView(mv);
}
}
}