/
iOSPlatformProvider.cs
165 lines (138 loc) · 5.31 KB
/
iOSPlatformProvider.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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Foundation;
using UIKit;
namespace Caliburn.Micro
{
/// <summary>
/// A <see cref="IPlatformProvider"/> implementation for the Xamarin iOS platfrom.
/// </summary>
public class IOSPlatformProvider : IPlatformProvider
{
private bool CheckAccess() {
return NSThread.IsMain;
}
/// <summary>
/// Indicates whether or not the framework is in design-time mode.
/// </summary>
public bool InDesignMode
{
get { return false; }
}
/// <summary>
/// Executes the action on the UI thread asynchronously.
/// </summary>
/// <param name="action">The action to execute.</param>
public void BeginOnUIThread(Action action)
{
UIApplication.SharedApplication.InvokeOnMainThread(action);
}
/// <summary>
/// Executes the action on the UI thread asynchronously.
/// </summary>
/// <param name = "action">The action to execute.</param>
public Task OnUIThreadAsync(Action action)
{
var completionSource = new TaskCompletionSource<bool>();
UIApplication.SharedApplication.InvokeOnMainThread(() =>
{
try
{
action();
completionSource.SetResult(true);
}
catch (TaskCanceledException)
{
completionSource.SetCanceled();
}
catch (Exception ex)
{
completionSource.SetException(ex);
}
});
return completionSource.Task;
}
/// <summary>
/// Executes the action on the UI thread.
/// </summary>
/// <param name = "action">The action to execute.</param>
public void OnUIThread(Action action) {
if (CheckAccess())
action();
else
OnUIThreadAsync(action).Wait();
}
/// <summary>
/// Used to retrieve the root, non-framework-created view.
/// </summary>
/// <param name="view">The view to search.</param>
/// <returns>The root element that was not created by the framework.</returns>
/// <remarks>In certain instances the services create UI elements.
/// For example, if you ask the window manager to show a UserControl as a dialog, it creates a window to host the UserControl in.
/// The WindowManager marks that element as a framework-created element so that it can determine what it created vs. what was intended by the developer.
/// Calling GetFirstNonGeneratedView allows the framework to discover what the original element was.
/// </remarks>
public object GetFirstNonGeneratedView(object view)
{
return view;
}
/// <summary>
/// Executes the handler the fist time the view is loaded.
/// </summary>
/// <param name="view">The view.</param>
/// <param name="handler">The handler.</param>
public void ExecuteOnFirstLoad(object view, Action<object> handler)
{
var viewController = view as IUIViewController;
if (viewController != null) {
EventHandler created = null;
created = (s, e) =>
{
viewController.ViewLoaded -= created;
handler(view);
};
viewController.ViewLoaded += created;
}
}
/// <summary>
/// Executes the handler the next time the view's LayoutUpdated event fires.
/// </summary>
/// <param name="view">The view.</param>
/// <param name="handler">The handler.</param>
public void ExecuteOnLayoutUpdated(object view, Action<object> handler)
{
var viewController = view as IUIViewController;
if (viewController != null)
{
EventHandler appeared = null;
appeared = (s, e) =>
{
viewController.ViewAppeared -= appeared;
handler(view);
};
viewController.ViewAppeared += appeared;
}
}
/// <summary>
/// Get the close action for the specified view model.
/// </summary>
/// <param name="viewModel">The view model to close.</param>
/// <param name="views">The associated views.</param>
/// <param name="dialogResult">The dialog result.</param>
/// <returns>An <see cref="Action"/> to close the view model.</returns>
public Action GetViewCloseAction(object viewModel, ICollection<object> views, bool? dialogResult)
{
var child = viewModel as IChild;
if (child != null)
{
var conductor = child.Parent as IConductor;
if (conductor != null)
{
return () => conductor.CloseItem(viewModel);
}
}
return () => LogManager.GetLog(typeof(Screen)).Info("TryClose requires a parent IConductor or a view with a Close method or IsOpen property.");
}
}
}