/
Platform.cs
276 lines (220 loc) · 7.43 KB
/
Platform.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform.WPF.Controls;
namespace Xamarin.Forms.Platform.WPF
{
public class Platform : BindableObject, INavigation
#pragma warning disable CS0618
, IPlatform
#pragma warning restore
{
readonly FormsApplicationPage _page;
Page Page { get; set; }
internal static readonly BindableProperty RendererProperty = BindableProperty.CreateAttached("Renderer", typeof(IVisualElementRenderer), typeof(Platform), default(IVisualElementRenderer));
internal Platform(FormsApplicationPage page)
{
_page = page;
var busyCount = 0;
MessagingCenter.Subscribe(this, Page.BusySetSignalName, (Page sender, bool enabled) =>
{
busyCount = Math.Max(0, enabled ? busyCount + 1 : busyCount - 1);
});
MessagingCenter.Subscribe<Page, AlertArguments>(this, Page.AlertSignalName, OnPageAlert);
MessagingCenter.Subscribe<Page, ActionSheetArguments>(this, Page.ActionSheetSignalName, OnPageActionSheet);
}
async void OnPageAlert(Page sender, AlertArguments options)
{
string content = options.Message ?? options.Title ?? string.Empty;
FormsContentDialog dialog = new FormsContentDialog();
if (options.Message == null || options.Title == null)
dialog.Content = content;
else
{
dialog.Title = options.Title;
dialog.Content = options.Message;
}
if (options.Accept != null)
{
dialog.IsPrimaryButtonEnabled = true;
dialog.PrimaryButtonText = options.Accept;
}
if (options.Cancel != null)
{
dialog.IsSecondaryButtonEnabled = true;
dialog.SecondaryButtonText = options.Cancel;
}
var dialogResult = await dialog.ShowAsync();
options.SetResult(dialogResult == LightContentDialogResult.Primary);
}
async void OnPageActionSheet(Page sender, ActionSheetArguments options)
{
var list = new System.Windows.Controls.ListView
{
Style = (System.Windows.Style)System.Windows.Application.Current.Resources["ActionSheetList"],
ItemsSource = options.Buttons
};
var dialog = new FormsContentDialog
{
Content = list,
};
if (options.Title != null)
dialog.Title = options.Title;
list.SelectionChanged += (s, e) =>
{
if(list.SelectedItem != null)
{
dialog.Hide();
options.SetResult((string)list.SelectedItem);
}
};
/*_page.KeyDown += (window, e) =>
{
if (e.Key == System.Windows.Input.Key.Escape)
{
dialog.Hide();
options.SetResult(LightContentDialogResult.None.ToString());
}
};*/
if (options.Cancel != null)
{
dialog.IsSecondaryButtonEnabled = true;
dialog.SecondaryButtonText = options.Cancel;
}
if (options.Destruction != null)
{
dialog.IsPrimaryButtonEnabled = true;
dialog.PrimaryButtonText = options.Destruction;
}
LightContentDialogResult result = await dialog.ShowAsync();
if (result == LightContentDialogResult.Secondary)
options.SetResult(options.Cancel);
else if (result == LightContentDialogResult.Primary)
options.SetResult(options.Destruction);
}
public static SizeRequest GetNativeSize(VisualElement view, double widthConstraint, double heightConstraint)
{
if (widthConstraint > 0 && heightConstraint > 0 && GetRenderer(view) != null)
{
IVisualElementRenderer element = GetRenderer(view);
return element.GetDesiredSize(widthConstraint, heightConstraint);
}
return new SizeRequest();
}
public static IVisualElementRenderer GetOrCreateRenderer(VisualElement element)
{
if (GetRenderer(element) == null)
SetRenderer(element, CreateRenderer(element));
return GetRenderer(element);
}
public static IVisualElementRenderer CreateRenderer(VisualElement element)
{
IVisualElementRenderer result = Registrar.Registered.GetHandlerForObject<IVisualElementRenderer>(element) ?? new DefaultViewRenderer();
result.SetElement(element);
return result;
}
public static IVisualElementRenderer GetRenderer(VisualElement self)
{
return (IVisualElementRenderer)self.GetValue(RendererProperty);
}
public static void SetRenderer(VisualElement self, IVisualElementRenderer renderer)
{
self.SetValue(RendererProperty, renderer);
self.IsPlatformEnabled = renderer != null;
}
internal void SetPage(Page newRoot)
{
if (newRoot == null)
return;
Page = newRoot;
#pragma warning disable CS0618 // Type or member is obsolete
// The Platform property is no longer necessary, but we have to set it because some third-party
// library might still be retrieving it and using it
Page.Platform = this;
#pragma warning restore CS0618 // Type or member is obsolete
_page.StartupPage = Page;
Application.Current.NavigationProxy.Inner = this;
}
public IReadOnlyList<Page> NavigationStack
{
get { throw new InvalidOperationException("NavigationStack is not supported globally on Windows, please use a NavigationPage."); }
}
public IReadOnlyList<Page> ModalStack
{
get
{
return _page.InternalChildren.Cast<Page>().ToList(); }
}
Task INavigation.PushAsync(Page root)
{
return ((INavigation)this).PushAsync(root, true);
}
Task<Page> INavigation.PopAsync()
{
return ((INavigation)this).PopAsync(true);
}
Task INavigation.PopToRootAsync()
{
return ((INavigation)this).PopToRootAsync(true);
}
Task INavigation.PushAsync(Page root, bool animated)
{
throw new InvalidOperationException("PushAsync is not supported globally on Windows, please use a NavigationPage.");
}
Task<Page> INavigation.PopAsync(bool animated)
{
throw new InvalidOperationException("PopAsync is not supported globally on Windows, please use a NavigationPage.");
}
Task INavigation.PopToRootAsync(bool animated)
{
throw new InvalidOperationException("PopToRootAsync is not supported globally on Windows, please use a NavigationPage.");
}
void INavigation.RemovePage(Page page)
{
throw new InvalidOperationException("RemovePage is not supported globally on Windows, please use a NavigationPage.");
}
void INavigation.InsertPageBefore(Page page, Page before)
{
throw new InvalidOperationException("InsertPageBefore is not supported globally on Windows, please use a NavigationPage.");
}
Task INavigation.PushModalAsync(Page page)
{
return ((INavigation)this).PushModalAsync(page, true);
}
Task<Page> INavigation.PopModalAsync()
{
return ((INavigation)this).PopModalAsync(true);
}
Task INavigation.PushModalAsync(Page page, bool animated)
{
if (page == null)
throw new ArgumentNullException(nameof(page));
var tcs = new TaskCompletionSource<bool>();
#pragma warning disable CS0618 // Type or member is obsolete
// The Platform property is no longer necessary, but we have to set it because some third-party
// library might still be retrieving it and using it
page.Platform = this;
#pragma warning restore CS0618 // Type or member is obsolete
_page.PushModal(page, animated);
tcs.SetResult(true);
return tcs.Task;
}
Task<Page> INavigation.PopModalAsync(bool animated)
{
var tcs = new TaskCompletionSource<Page>();
var page =_page.PopModal(animated) as Page;
tcs.SetResult(page);
return tcs.Task;
}
#region Obsolete
SizeRequest IPlatform.GetNativeSize(VisualElement view, double widthConstraint, double heightConstraint)
{
return GetNativeSize(view, widthConstraint, heightConstraint);
}
#endregion
}
}