-
Notifications
You must be signed in to change notification settings - Fork 163
/
Shell.xaml.cs
106 lines (97 loc) · 3.48 KB
/
Shell.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace Template10.Views
{
public sealed partial class Shell : Page
{
public Shell(Frame frame)
{
this.InitializeComponent();
this.ShellSplitView.Content = frame;
var update = new Action(() =>
{
// update radiobuttons after frame navigates
var type = frame.CurrentSourcePageType;
foreach (var radioButton in AllRadioButtons(this))
{
var target = radioButton.CommandParameter as NavType;
if (target == null)
continue;
radioButton.IsChecked = target.Type.Equals(type);
}
this.ShellSplitView.IsPaneOpen = false;
this.BackCommand.RaiseCanExecuteChanged();
});
frame.Navigated += (s, e) => update();
this.Loaded += (s, e) => update();
this.DataContext = this;
}
// back
Mvvm.Command _backCommand;
public Mvvm.Command BackCommand { get { return _backCommand ?? (_backCommand = new Mvvm.Command(ExecuteBack, CanBack)); } }
private bool CanBack()
{
var nav = (App.Current as App).NavigationService;
return nav.CanGoBack;
}
private void ExecuteBack()
{
var nav = (App.Current as App).NavigationService;
nav.GoBack();
}
// menu
Mvvm.Command _menuCommand;
public Mvvm.Command MenuCommand { get { return _menuCommand ?? (_menuCommand = new Mvvm.Command(ExecuteMenu)); } }
private void ExecuteMenu()
{
this.ShellSplitView.IsPaneOpen = !this.ShellSplitView.IsPaneOpen;
}
// nav
Mvvm.Command<NavType> _navCommand;
public Mvvm.Command<NavType> NavCommand { get { return _navCommand ?? (_navCommand = new Mvvm.Command<NavType>(ExecuteNav)); } }
private void ExecuteNav(NavType navType)
{
var type = navType.Type;
var nav = (App.Current as App).NavigationService;
// when we nav home, clear history
if (type.Equals(typeof(Views.MainPage)))
nav.ClearHistory();
// navigate only to new pages
if (nav.CurrentPageType != null && nav.CurrentPageType != type)
nav.Navigate(type, navType.Parameter);
}
// utility
public List<RadioButton> AllRadioButtons(DependencyObject parent)
{
var list = new List<RadioButton>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is RadioButton)
{
list.Add(child as RadioButton);
continue;
}
list.AddRange(AllRadioButtons(child));
}
return list;
}
// prevent check
private void DontCheck(object s, RoutedEventArgs e)
{
// don't let the radiobutton check
(s as RadioButton).IsChecked = false;
}
}
public class NavType
{
public Type Type { get; set; }
public string Parameter { get; set; }
}
}