-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppShell.xaml.cs
305 lines (270 loc) · 9.13 KB
/
AppShell.xaml.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Metadata;
using Windows.UI.Xaml.Automation;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using NavigationMenuSample.Controls;
using NavigationMenuSample.Views;
namespace NavigationMenuSample
{
/// <summary>
/// The "chrome" layer of the app that provides top-level navigation with
/// proper keyboarding navigation.
/// </summary>
public sealed partial class AppShell : Page
{
// Declare the top level nav items
private List<NavMenuItem> navlist = new List<NavMenuItem>(
new[]
{
new NavMenuItem()
{
Symbol = Symbol.Home,
Label = "Home",
DestPage = typeof(BasicPage)
},
new NavMenuItem()
{
Symbol = Symbol.View,
Label = "Feed",
DestPage = typeof(CommandBarPage)
},
new NavMenuItem()
{
Symbol = Symbol.Shop,
Label = "Shopping Tweet",
DestPage = typeof(DrillInPage)
},
});
public static AppShell Current = null;
/// <summary>
/// Initializes a new instance of the AppShell, sets the static 'Current' reference,
/// adds callbacks for Back requests and changes in the SplitView's DisplayMode, and
/// provide the nav menu list with the data to display.
/// </summary>
public AppShell()
{
this.InitializeComponent();
this.Loaded += (sender, args) =>
{
Current = this;
this.TogglePaneButton.Focus(FocusState.Programmatic);
};
SystemNavigationManager.GetForCurrentView().BackRequested += SystemNavigationManager_BackRequested;
// If on a phone device that has hardware buttons then we hide the app's back button.
if (ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
{
this.BackButton.Visibility = Visibility.Collapsed;
}
NavMenuList.ItemsSource = navlist;
}
public Frame AppFrame { get { return this.frame; } }
/// <summary>
/// Default keyboard focus movement for any unhandled keyboarding
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AppShell_KeyDown(object sender, KeyRoutedEventArgs e)
{
FocusNavigationDirection direction = FocusNavigationDirection.None;
switch (e.Key)
{
case Windows.System.VirtualKey.Left:
case Windows.System.VirtualKey.GamepadDPadLeft:
case Windows.System.VirtualKey.GamepadLeftThumbstickLeft:
case Windows.System.VirtualKey.NavigationLeft:
direction = FocusNavigationDirection.Left;
break;
case Windows.System.VirtualKey.Right:
case Windows.System.VirtualKey.GamepadDPadRight:
case Windows.System.VirtualKey.GamepadLeftThumbstickRight:
case Windows.System.VirtualKey.NavigationRight:
direction = FocusNavigationDirection.Right;
break;
case Windows.System.VirtualKey.Up:
case Windows.System.VirtualKey.GamepadDPadUp:
case Windows.System.VirtualKey.GamepadLeftThumbstickUp:
case Windows.System.VirtualKey.NavigationUp:
direction = FocusNavigationDirection.Up;
break;
case Windows.System.VirtualKey.Down:
case Windows.System.VirtualKey.GamepadDPadDown:
case Windows.System.VirtualKey.GamepadLeftThumbstickDown:
case Windows.System.VirtualKey.NavigationDown:
direction = FocusNavigationDirection.Down;
break;
}
if (direction != FocusNavigationDirection.None)
{
var control = FocusManager.FindNextFocusableElement(direction) as Control;
if (control != null)
{
control.Focus(FocusState.Programmatic);
e.Handled = true;
}
}
}
#region BackRequested Handlers
private void SystemNavigationManager_BackRequested(object sender, BackRequestedEventArgs e)
{
bool handled = e.Handled;
this.BackRequested(ref handled);
e.Handled = handled;
}
private void BackButton_Click(object sender, RoutedEventArgs e)
{
bool ignored = false;
this.BackRequested(ref ignored);
}
private void BackRequested(ref bool handled)
{
// Get a hold of the current frame so that we can inspect the app back stack.
if (this.AppFrame == null)
return;
// Check to see if this is the top-most page on the app back stack.
if (this.AppFrame.CanGoBack && !handled)
{
// If not, set the event to handled and go back to the previous page in the app.
handled = true;
this.AppFrame.GoBack();
}
}
#endregion
#region Navigation
/// <summary>
/// Navigate to the Page for the selected <paramref name="listViewItem"/>.
/// </summary>
/// <param name="sender"></param>
/// <param name="listViewItem"></param>
private void NavMenuList_ItemInvoked(object sender, ListViewItem listViewItem)
{
var item = (NavMenuItem)((NavMenuListView)sender).ItemFromContainer(listViewItem);
if (item != null)
{
if (item.DestPage != null &&
item.DestPage != this.AppFrame.CurrentSourcePageType)
{
this.AppFrame.Navigate(item.DestPage, item.Arguments);
}
}
}
/// <summary>
/// Ensures the nav menu reflects reality when navigation is triggered outside of
/// the nav menu buttons.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnNavigatingToPage(object sender, NavigatingCancelEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back)
{
var item = (from p in this.navlist where p.DestPage == e.SourcePageType select p).SingleOrDefault();
if (item == null && this.AppFrame.BackStackDepth > 0)
{
// In cases where a page drills into sub-pages then we'll highlight the most recent
// navigation menu item that appears in the BackStack
foreach (var entry in this.AppFrame.BackStack.Reverse())
{
item = (from p in this.navlist where p.DestPage == entry.SourcePageType select p).SingleOrDefault();
if (item != null)
break;
}
}
var container = (ListViewItem)NavMenuList.ContainerFromItem(item);
// While updating the selection state of the item prevent it from taking keyboard focus. If a
// user is invoking the back button via the keyboard causing the selected nav menu item to change
// then focus will remain on the back button.
if (container != null) container.IsTabStop = false;
NavMenuList.SetSelectedItem(container);
if (container != null) container.IsTabStop = true;
}
}
private void OnNavigatedToPage(object sender, NavigationEventArgs e)
{
// After a successful navigation set keyboard focus to the loaded page
if (e.Content is Page && e.Content != null)
{
var control = (Page)e.Content;
control.Loaded += Page_Loaded;
}
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
((Page)sender).Focus(FocusState.Programmatic);
((Page)sender).Loaded -= Page_Loaded;
}
#endregion
public Rect TogglePaneButtonRect
{
get;
private set;
}
/// <summary>
/// An event to notify listeners when the hamburger button may occlude other content in the app.
/// The custom "PageHeader" user control is using this.
/// </summary>
public event TypedEventHandler<AppShell, Rect> TogglePaneButtonRectChanged;
/// <summary>
/// Callback when the SplitView's Pane is toggled open or close. When the Pane is not visible
/// then the floating hamburger may be occluding other content in the app unless it is aware.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TogglePaneButton_Checked(object sender, RoutedEventArgs e)
{
this.CheckTogglePaneButtonSizeChanged();
}
/// <summary>
/// Check for the conditions where the navigation pane does not occupy the space under the floating
/// hamburger button and trigger the event.
/// </summary>
private void CheckTogglePaneButtonSizeChanged()
{
if (this.RootSplitView.DisplayMode == SplitViewDisplayMode.Inline ||
this.RootSplitView.DisplayMode == SplitViewDisplayMode.Overlay)
{
var transform = this.TogglePaneButton.TransformToVisual(this);
var rect = transform.TransformBounds(new Rect(0, 0, this.TogglePaneButton.ActualWidth, this.TogglePaneButton.ActualHeight));
this.TogglePaneButtonRect = rect;
}
else
{
this.TogglePaneButtonRect = new Rect();
}
var handler = this.TogglePaneButtonRectChanged;
if (handler != null)
{
// handler(this, this.TogglePaneButtonRect);
handler.DynamicInvoke(this, this.TogglePaneButtonRect);
}
}
/// <summary>
/// Enable accessibility on each nav menu item by setting the AutomationProperties.Name on each container
/// using the associated Label of each item.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void NavMenuItemContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
{
if (!args.InRecycleQueue && args.Item != null && args.Item is NavMenuItem)
{
args.ItemContainer.SetValue(AutomationProperties.NameProperty, ((NavMenuItem)args.Item).Label);
}
else
{
args.ItemContainer.ClearValue(AutomationProperties.NameProperty);
}
}
}
}