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
|
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Xamarin.Forms.PlatformConfiguration.WindowsSpecific;
#if WINDOWS_UWP
namespace Xamarin.Forms.Platform.UWP
#else
namespace Xamarin.Forms.Platform.WinRT
#endif
{
public sealed partial class PageControl : IToolbarProvider
{
public static readonly DependencyProperty InvisibleBackButtonCollapsedProperty = DependencyProperty.Register("InvisibleBackButtonCollapsed", typeof(bool), typeof(PageControl),
new PropertyMetadata(true, OnInvisibleBackButtonCollapsedChanged));
public static readonly DependencyProperty ShowBackButtonProperty = DependencyProperty.Register("ShowBackButton", typeof(bool), typeof(PageControl),
new PropertyMetadata(false, OnShowBackButtonChanged));
public static readonly DependencyProperty TitleVisibilityProperty = DependencyProperty.Register(nameof(TitleVisibility), typeof(Visibility), typeof(PageControl), new PropertyMetadata(Visibility.Visible));
public static readonly DependencyProperty ToolbarBackgroundProperty = DependencyProperty.Register(nameof(ToolbarBackground), typeof(Brush), typeof(PageControl),
new PropertyMetadata(default(Brush)));
public static readonly DependencyProperty BackButtonTitleProperty = DependencyProperty.Register("BackButtonTitle", typeof(string), typeof(PageControl), new PropertyMetadata(false));
public static readonly DependencyProperty ContentMarginProperty = DependencyProperty.Register("ContentMargin", typeof(Windows.UI.Xaml.Thickness), typeof(PageControl),
new PropertyMetadata(default(Windows.UI.Xaml.Thickness)));
public static readonly DependencyProperty TitleInsetProperty = DependencyProperty.Register("TitleInset", typeof(double), typeof(PageControl), new PropertyMetadata(default(double)));
public static readonly DependencyProperty TitleBrushProperty = DependencyProperty.Register("TitleBrush", typeof(Brush), typeof(PageControl), new PropertyMetadata(null));
AppBarButton _backButton;
CommandBar _commandBar;
#if WINDOWS_UWP
ToolbarPlacement _toolbarPlacement;
Border _bottomCommandBarArea;
Border _topCommandBarArea;
#endif
TaskCompletionSource<CommandBar> _commandBarTcs;
Windows.UI.Xaml.Controls.ContentPresenter _presenter;
public PageControl()
{
InitializeComponent();
}
public string BackButtonTitle
{
get { return (string)GetValue(BackButtonTitleProperty); }
set { SetValue(BackButtonTitleProperty, value); }
}
public double ContentHeight
{
get { return _presenter != null ? _presenter.ActualHeight : 0; }
}
public Windows.UI.Xaml.Thickness ContentMargin
{
get { return (Windows.UI.Xaml.Thickness)GetValue(ContentMarginProperty); }
set { SetValue(ContentMarginProperty, value); }
}
public double ContentWidth
{
get { return _presenter != null ? _presenter.ActualWidth : 0; }
}
public bool InvisibleBackButtonCollapsed
{
get { return (bool)GetValue(InvisibleBackButtonCollapsedProperty); }
set { SetValue(InvisibleBackButtonCollapsedProperty, value); }
}
public Brush ToolbarBackground
{
get { return (Brush)GetValue(ToolbarBackgroundProperty); }
set { SetValue(ToolbarBackgroundProperty, value); }
}
#if WINDOWS_UWP
public ToolbarPlacement ToolbarPlacement
{
get { return _toolbarPlacement; }
set
{
_toolbarPlacement = value;
UpdateToolbarPlacement();
}
}
#endif
public bool ShowBackButton
{
get { return (bool)GetValue(ShowBackButtonProperty); }
set { SetValue(ShowBackButtonProperty, value); }
}
public Visibility TitleVisibility
{
get { return (Visibility)GetValue(TitleVisibilityProperty); }
set { SetValue(TitleVisibilityProperty, value); }
}
public Brush TitleBrush
{
get { return (Brush)GetValue(TitleBrushProperty); }
set { SetValue(TitleBrushProperty, value); }
}
public double TitleInset
{
get { return (double)GetValue(TitleInsetProperty); }
set { SetValue(TitleInsetProperty, value); }
}
Task<CommandBar> IToolbarProvider.GetCommandBarAsync()
{
if (_commandBar != null)
return Task.FromResult(_commandBar);
_commandBarTcs = new TaskCompletionSource<CommandBar>();
ApplyTemplate();
return _commandBarTcs.Task;
}
public event RoutedEventHandler BackClicked;
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
_backButton = GetTemplateChild("backButton") as AppBarButton;
if (_backButton != null)
_backButton.Click += OnBackClicked;
_presenter = GetTemplateChild("presenter") as Windows.UI.Xaml.Controls.ContentPresenter;
_commandBar = GetTemplateChild("CommandBar") as CommandBar;
#if WINDOWS_UWP
_bottomCommandBarArea = GetTemplateChild("BottomCommandBarArea") as Border;
_topCommandBarArea = GetTemplateChild("TopCommandBarArea") as Border;
if (_commandBar != null && _bottomCommandBarArea != null && _topCommandBarArea != null)
{
// We have to wait for the command bar to load so that it'll be in the control hierarchy
// otherwise we can't properly move it to wherever the toolbar is supposed to be
_commandBar.Loaded += (sender, args) => UpdateToolbarPlacement();
}
#endif
TaskCompletionSource<CommandBar> tcs = _commandBarTcs;
tcs?.SetResult(_commandBar);
}
void OnBackClicked(object sender, RoutedEventArgs e)
{
RoutedEventHandler clicked = BackClicked;
if (clicked != null)
clicked(this, e);
}
static void OnInvisibleBackButtonCollapsedChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
((PageControl)dependencyObject).UpdateBackButton();
}
static void OnShowBackButtonChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
((PageControl)dependencyObject).UpdateBackButton();
}
void UpdateBackButton()
{
if (_backButton == null)
return;
if (ShowBackButton)
_backButton.Visibility = Visibility.Visible;
else
_backButton.Visibility = InvisibleBackButtonCollapsed ? Visibility.Collapsed : Visibility.Visible;
_backButton.Opacity = ShowBackButton ? 1 : 0;
}
#if WINDOWS_UWP
void UpdateToolbarPlacement()
{
ToolbarPlacementHelper.UpdateToolbarPlacement(_commandBar, ToolbarPlacement, _bottomCommandBarArea, _topCommandBarArea);
}
#endif
}
}
|