forked from Esri/arcgis-maps-sdk-dotnet-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestoreAutoPanMode.cs
178 lines (158 loc) · 5.54 KB
/
RestoreAutoPanMode.cs
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using Esri.ArcGISRuntime.UI;
using System;
using System.Threading;
using System.Threading.Tasks;
#if MAUI
using Esri.ArcGISRuntime.Maui;
#elif NETFX_CORE
using Esri.ArcGISRuntime.UI.Controls;
using Windows.UI.Xaml;
#else
using Esri.ArcGISRuntime.UI.Controls;
using System.Windows;
#endif
namespace RoutingSample
{
public class RestoreAutoPanMode
{
private MapView _mapView;
private CancellationTokenSource _delayTokenSource;
private CancellationToken _delayToken;
private void AttachToMapView(MapView mv)
{
if (_mapView != null && _mapView != mv)
throw new InvalidOperationException("RestoreAutoPanMode can only be assigned to one MapView.");
_mapView = mv;
_mapView.NavigationCompleted += OnMapViewNavigationCompleted;
}
private void DetachFromMapView(MapView mv)
{
if (_mapView != null && _mapView == mv)
{
_mapView.NavigationCompleted -= OnMapViewNavigationCompleted;
_mapView = null;
}
}
private async void OnMapViewNavigationCompleted(object sender, EventArgs e)
{
// If user stopped navigating and we're not in the correct autopan mode,
// restore autopan after the set delay.
if (IsEnabled && !_mapView.IsNavigating)
{
if (_mapView.LocationDisplay != null && _mapView.LocationDisplay.AutoPanMode != PanMode)
{
if (_delayTokenSource != null)
{
if (_delayToken.CanBeCanceled)
_delayTokenSource.Cancel();
_delayTokenSource.Dispose();
}
_delayTokenSource = new CancellationTokenSource();
_delayToken = _delayTokenSource.Token;
try
{
await WaitAndResetPanMode();
}
catch (TaskCanceledException)
{ }
}
}
}
private async Task WaitAndResetPanMode()
{
await Task.Delay(DelayInSeconds * 1000, _delayToken);
if (!_delayToken.IsCancellationRequested)
ResetPanMode();
}
private void ResetPanMode()
{
if (_mapView != null && _mapView.LocationDisplay != null)
_mapView.LocationDisplay.AutoPanMode = PanMode;
}
/// <summary>
/// Gets or sets whether the property is enabled.
/// </summary>
public bool IsEnabled { get; set; }
/// <summary>
/// Gets or sets the delay in seconds.
/// </summary>
public int DelayInSeconds { get; set; }
/// <summary>
/// Gets or sets the pan mode.
/// </summary>
public LocationDisplayAutoPanMode PanMode { get; set; }
#if MAUI
public static BindableProperty RestoreAutoPanSettingsProperty =
BindableProperty.CreateAttached(
"RestoreAutoPanSettings",
typeof(RestoreAutoPanMode),
typeof(RestoreAutoPanMode),
null,
propertyChanged: OnRestoreAutoPanSettingsChanged
);
#else
public static readonly DependencyProperty RestoreAutoPanSettingsProperty =
DependencyProperty.RegisterAttached(
"RestoreAutoPanSettings",
typeof(RestoreAutoPanMode),
typeof(RestoreAutoPanMode),
new PropertyMetadata(null, OnRestoreAutoPanSettingsChanged)
);
#endif
#if MAUI
public static RestoreAutoPanMode GetRestoreAutoPanSettings(BindableObject obj)
#else
public static RestoreAutoPanMode GetRestoreAutoPanSettings(DependencyObject obj)
#endif
{
return (RestoreAutoPanMode)obj.GetValue(RestoreAutoPanSettingsProperty);
}
#if MAUI
public static void SetRestoreAutoPanSettings(BindableObject obj, RestoreAutoPanMode value)
#else
public static void SetRestoreAutoPanSettings(DependencyObject obj, RestoreAutoPanMode value)
#endif
{
obj.SetValue(RestoreAutoPanSettingsProperty, value);
}
#if MAUI
private static void OnRestoreAutoPanSettingsChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is MapView mv)
{
if (oldValue is RestoreAutoPanMode restoreOld)
{
restoreOld.DetachFromMapView(mv);
}
if (newValue is RestoreAutoPanMode restoreNew)
{
restoreNew.AttachToMapView(mv);
}
}
else
{
throw new InvalidOperationException("This property must be attached to a MapView.");
}
}
#else
private static void OnRestoreAutoPanSettingsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is MapView mv)
{
if (e.OldValue is RestoreAutoPanMode oldValue)
{
oldValue.DetachFromMapView(mv);
}
if (e.NewValue is RestoreAutoPanMode newValue)
{
newValue.AttachToMapView(mv);
}
}
else
{
throw new InvalidOperationException("This property must be attached to a MapView.");
}
}
#endif
}
}