-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
RegionNavigationService.cs
315 lines (272 loc) · 12.5 KB
/
RegionNavigationService.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using CommonServiceLocator;
using Prism.Common;
using Prism.Properties;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Windows;
namespace Prism.Regions
{
/// <summary>
/// Provides navigation for regions.
/// </summary>
public class RegionNavigationService : IRegionNavigationService
{
private readonly IServiceLocator serviceLocator;
private readonly IRegionNavigationContentLoader regionNavigationContentLoader;
private IRegionNavigationJournal journal;
private NavigationContext currentNavigationContext;
/// <summary>
/// Initializes a new instance of the <see cref="RegionNavigationService"/> class.
/// </summary>
/// <param name="serviceLocator">The service locator.</param>
/// <param name="regionNavigationContentLoader">The navigation target handler.</param>
/// <param name="journal">The journal.</param>
public RegionNavigationService(IServiceLocator serviceLocator, IRegionNavigationContentLoader regionNavigationContentLoader, IRegionNavigationJournal journal)
{
if (serviceLocator == null)
throw new ArgumentNullException(nameof(serviceLocator));
if (regionNavigationContentLoader == null)
throw new ArgumentNullException(nameof(regionNavigationContentLoader));
if (journal == null)
throw new ArgumentNullException(nameof(journal));
this.serviceLocator = serviceLocator;
this.regionNavigationContentLoader = regionNavigationContentLoader;
this.journal = journal;
this.journal.NavigationTarget = this;
}
/// <summary>
/// Gets or sets the region.
/// </summary>
/// <value>The region.</value>
public IRegion Region { get; set; }
/// <summary>
/// Gets the journal.
/// </summary>
/// <value>The journal.</value>
public IRegionNavigationJournal Journal
{
get
{
return this.journal;
}
}
/// <summary>
/// Raised when the region is about to be navigated to content.
/// </summary>
public event EventHandler<RegionNavigationEventArgs> Navigating;
private void RaiseNavigating(NavigationContext navigationContext)
{
if (this.Navigating != null)
{
this.Navigating(this, new RegionNavigationEventArgs(navigationContext));
}
}
/// <summary>
/// Raised when the region is navigated to content.
/// </summary>
public event EventHandler<RegionNavigationEventArgs> Navigated;
private void RaiseNavigated(NavigationContext navigationContext)
{
if (this.Navigated != null)
{
this.Navigated(this, new RegionNavigationEventArgs(navigationContext));
}
}
/// <summary>
/// Raised when a navigation request fails.
/// </summary>
public event EventHandler<RegionNavigationFailedEventArgs> NavigationFailed;
private void RaiseNavigationFailed(NavigationContext navigationContext, Exception error)
{
if (this.NavigationFailed != null)
{
this.NavigationFailed(this, new RegionNavigationFailedEventArgs(navigationContext, error));
}
}
/// <summary>
/// Initiates navigation to the specified target.
/// </summary>
/// <param name="target">The target.</param>
/// <param name="navigationCallback">A callback to execute when the navigation request is completed.</param>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exception is marshalled to callback")]
public void RequestNavigate(Uri target, Action<NavigationResult> navigationCallback)
{
this.RequestNavigate(target, navigationCallback, null);
}
/// <summary>
/// Initiates navigation to the specified target.
/// </summary>
/// <param name="target">The target.</param>
/// <param name="navigationCallback">A callback to execute when the navigation request is completed.</param>
/// <param name="navigationParameters">The navigation parameters specific to the navigation request.</param>
public void RequestNavigate(Uri target, Action<NavigationResult> navigationCallback, NavigationParameters navigationParameters)
{
if (navigationCallback == null)
throw new ArgumentNullException(nameof(navigationCallback));
try
{
this.DoNavigate(target, navigationCallback, navigationParameters);
}
catch (Exception e)
{
this.NotifyNavigationFailed(new NavigationContext(this, target), navigationCallback, e);
}
}
private void DoNavigate(Uri source, Action<NavigationResult> navigationCallback, NavigationParameters navigationParameters)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (this.Region == null)
throw new InvalidOperationException(Resources.NavigationServiceHasNoRegion);
this.currentNavigationContext = new NavigationContext(this, source, navigationParameters);
// starts querying the active views
RequestCanNavigateFromOnCurrentlyActiveView(
this.currentNavigationContext,
navigationCallback,
this.Region.ActiveViews.ToArray(),
0);
}
private void RequestCanNavigateFromOnCurrentlyActiveView(
NavigationContext navigationContext,
Action<NavigationResult> navigationCallback,
object[] activeViews,
int currentViewIndex)
{
if (currentViewIndex < activeViews.Length)
{
var vetoingView = activeViews[currentViewIndex] as IConfirmNavigationRequest;
if (vetoingView != null)
{
// the current active view implements IConfirmNavigationRequest, request confirmation
// providing a callback to resume the navigation request
vetoingView.ConfirmNavigationRequest(
navigationContext,
canNavigate =>
{
if (this.currentNavigationContext == navigationContext && canNavigate)
{
RequestCanNavigateFromOnCurrentlyActiveViewModel(
navigationContext,
navigationCallback,
activeViews,
currentViewIndex);
}
else
{
this.NotifyNavigationFailed(navigationContext, navigationCallback, null);
}
});
}
else
{
RequestCanNavigateFromOnCurrentlyActiveViewModel(
navigationContext,
navigationCallback,
activeViews,
currentViewIndex);
}
}
else
{
ExecuteNavigation(navigationContext, activeViews, navigationCallback);
}
}
private void RequestCanNavigateFromOnCurrentlyActiveViewModel(
NavigationContext navigationContext,
Action<NavigationResult> navigationCallback,
object[] activeViews,
int currentViewIndex)
{
var frameworkElement = activeViews[currentViewIndex] as FrameworkElement;
if (frameworkElement != null)
{
var vetoingViewModel = frameworkElement.DataContext as IConfirmNavigationRequest;
if (vetoingViewModel != null)
{
// the data model for the current active view implements IConfirmNavigationRequest, request confirmation
// providing a callback to resume the navigation request
vetoingViewModel.ConfirmNavigationRequest(
navigationContext,
canNavigate =>
{
if (this.currentNavigationContext == navigationContext && canNavigate)
{
RequestCanNavigateFromOnCurrentlyActiveView(
navigationContext,
navigationCallback,
activeViews,
currentViewIndex + 1);
}
else
{
this.NotifyNavigationFailed(navigationContext, navigationCallback, null);
}
});
return;
}
}
RequestCanNavigateFromOnCurrentlyActiveView(
navigationContext,
navigationCallback,
activeViews,
currentViewIndex + 1);
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exception is marshalled to callback")]
private void ExecuteNavigation(NavigationContext navigationContext, object[] activeViews, Action<NavigationResult> navigationCallback)
{
try
{
NotifyActiveViewsNavigatingFrom(navigationContext, activeViews);
object view = this.regionNavigationContentLoader.LoadContent(this.Region, navigationContext);
// Raise the navigating event just before activing the view.
this.RaiseNavigating(navigationContext);
this.Region.Activate(view);
// Update the navigation journal before notifying others of navigaton
IRegionNavigationJournalEntry journalEntry = this.serviceLocator.GetInstance<IRegionNavigationJournalEntry>();
journalEntry.Uri = navigationContext.Uri;
journalEntry.Parameters = navigationContext.Parameters;
bool persistInHistory = PersistInHistory(activeViews);
this.journal.RecordNavigation(journalEntry, persistInHistory);
// The view can be informed of navigation
Action<INavigationAware> action = (n) => n.OnNavigatedTo(navigationContext);
MvvmHelpers.ViewAndViewModelAction(view, action);
navigationCallback(new NavigationResult(navigationContext, true));
// Raise the navigated event when navigation is completed.
this.RaiseNavigated(navigationContext);
}
catch (Exception e)
{
this.NotifyNavigationFailed(navigationContext, navigationCallback, e);
}
}
private static bool PersistInHistory(object[] activeViews)
{
bool persist = true;
if (activeViews.Length > 0)
{
MvvmHelpers.ViewAndViewModelAction<IJournalAware>(activeViews[0], ija => { persist &= ija.PersistInHistory(); });
}
return persist;
}
private void NotifyNavigationFailed(NavigationContext navigationContext, Action<NavigationResult> navigationCallback, Exception e)
{
var navigationResult =
e != null ? new NavigationResult(navigationContext, e) : new NavigationResult(navigationContext, false);
navigationCallback(navigationResult);
this.RaiseNavigationFailed(navigationContext, e);
}
private static void NotifyActiveViewsNavigatingFrom(NavigationContext navigationContext, object[] activeViews)
{
InvokeOnNavigationAwareElements(activeViews, (n) => n.OnNavigatedFrom(navigationContext));
}
private static void InvokeOnNavigationAwareElements(IEnumerable<object> items, Action<INavigationAware> invocation)
{
foreach (var item in items)
{
MvvmHelpers.ViewAndViewModelAction(item, invocation);
}
}
}
}