Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
BaseContentPage.cs
1using Microsoft.Maui.Controls.PlatformConfiguration;
2using Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;
6using Waher.Events;
8
10{
11 public abstract class BaseContentPage : ContentPage
12 {
17 {
18 set => this.BindingContext = value;
19 get => this.ViewModel<BaseViewModel>();
20 }
21
25 public T ViewModel<T>() where T : BaseViewModel
26 {
27 if (this.BindingContext is T ViewModel)
28 return ViewModel;
29
30 throw new ArgumentException("Wrong view model type: " + typeof(T).FullName);
31 }
32
37 public virtual string? UniqueId { get; set; }
38
43 : base()
44 {
45 this.On<iOS>().SetUseSafeArea(true);
46 this.SetBackgroundColor();
47 Microsoft.Maui.Controls.Application.Current!.RequestedThemeChanged += this.OnRequestedThemeChanged;
48
49
50 this.Loaded += this.BaseContentPage_Loaded;
51 }
52
53 private void OnRequestedThemeChanged(object? sender, AppThemeChangedEventArgs e)
54 {
55 // Update the background color when the theme changes
56 this.SetBackgroundColor();
57 }
58
59 private void SetBackgroundColor()
60 {
61 this.BackgroundColor = AppColors.PrimaryBackground;
62 }
63
64 private void BaseContentPage_Loaded(object? sender, EventArgs e)
65 {
66 try
67 {
68 this.OnLoaded();
69 }
70 catch (Exception ex)
71 {
72 ServiceRef.LogService.LogException(ex);
73 }
74 }
75
79 protected virtual void OnLoaded()
80 {
81 }
82
84 protected sealed override async void OnAppearing()
85 {
86 try
87 {
88 base.OnAppearing();
89
90 await this.OnAppearingAsync();
91 }
92 catch (Exception ex)
93 {
94 Log.Exception(ex);
95 }
96 }
97
101 protected virtual async Task OnAppearingAsync()
102 {
103 BaseViewModel ViewModel = this.ViewModel<BaseViewModel>();
104 bool DoAppearing = !ViewModel.IsAppearing;
105
106 if (DoAppearing)
107 {
108 EventHandlerAsync? BeforeAppearing = this.OnBeforeAppearing;
109
110 if (BeforeAppearing is not null)
111 {
112 try
113 {
114 await BeforeAppearing(this, EventArgs.Empty);
115 }
116 catch (Exception ex)
117 {
118 ServiceRef.LogService.LogException(ex);
119 }
120 }
121
122 try
123 {
124 await ViewModel.DoAppearing();
125 }
126 catch (Exception ex)
127 {
128 ex = Log.UnnestException(ex);
129 ServiceRef.LogService.LogException(ex);
130
131 string Message = ServiceRef.Localizer[nameof(AppResources.FailedToBindViewModelForPage),
132 ViewModel.GetType().FullName ?? string.Empty, this.GetType().FullName ?? string.Empty];
133
134 await ServiceRef.UiService.DisplayAlert(ServiceRef.Localizer[nameof(AppResources.ErrorTitle)], Message + Environment.NewLine + ex.Message);
135 }
136 }
137
138 try
139 {
140 if (await ServiceRef.SettingsService.WaitInitDone())
141 await ViewModel.RestoreState();
142 }
143 catch (Exception ex)
144 {
145 ex = Log.UnnestException(ex);
146 ServiceRef.LogService.LogException(ex);
147
148 string Message = ServiceRef.Localizer[nameof(AppResources.FailedToRestoreViewModelStateForPage),
149 ViewModel.GetType().FullName ?? string.Empty, this.GetType().FullName ?? string.Empty];
150
151 await ServiceRef.UiService.DisplayAlert(ServiceRef.Localizer[nameof(AppResources.ErrorTitle)], Message + Environment.NewLine + ex.Message);
152 }
153
154 if (DoAppearing)
155 {
156 EventHandlerAsync? AfterAppearing = this.OnAfterAppearing;
157
158 if (AfterAppearing is not null)
159 {
160 try
161 {
162 await AfterAppearing(this, EventArgs.Empty);
163 }
164 catch (Exception ex)
165 {
166 ServiceRef.LogService.LogException(ex);
167 }
168 }
169 }
170 }
171
176
181
183 protected sealed override async void OnDisappearing()
184 {
185 try
186 {
187 await this.OnDisappearingAsync();
188 base.OnDisappearing();
189 }
190 catch (Exception ex)
191 {
192 Log.Exception(ex);
193 }
194 }
195
199 protected virtual async Task OnDisappearingAsync()
200 {
201 BaseViewModel ViewModel = this.ViewModel<BaseViewModel>();
202
203 if (ViewModel.IsAppearing)
204 {
205 try
206 {
207 if (await ServiceRef.SettingsService.WaitInitDone())
208 await ViewModel.SaveState();
209 }
210 catch (Exception ex)
211 {
212 ex = Log.UnnestException(ex);
213 ServiceRef.LogService.LogException(ex);
214
215 string msg = ServiceRef.Localizer[nameof(AppResources.FailedToSaveViewModelStateForPage),
216 ViewModel.GetType().FullName ?? string.Empty, this.GetType().FullName ?? string.Empty];
217
218 await ServiceRef.UiService.DisplayAlert(
219 ServiceRef.Localizer[nameof(AppResources.ErrorTitle)],
220 msg + Environment.NewLine + ex.Message);
221 }
222 }
223
224 try
225 {
226 await ViewModel.DoDisappearing();
227 }
228 catch (Exception ex)
229 {
230 ex = Log.UnnestException(ex);
231 ServiceRef.LogService.LogException(ex);
232
233 string msg = ServiceRef.Localizer[nameof(AppResources.FailedToUnbindViewModelForPage),
234 ViewModel.GetType().FullName ?? string.Empty, this.GetType().FullName ?? string.Empty];
235
236 await ServiceRef.UiService.DisplayAlert(
237 ServiceRef.Localizer[nameof(AppResources.ErrorTitle)],
238 msg + Environment.NewLine + ex.Message);
239 }
240 }
241
246 protected sealed override bool OnBackButtonPressed()
247 {
248 try
249 {
250 BaseViewModel ViewModel = this.ViewModel<BaseViewModel>();
251
253 {
254 if (RegistrationViewModel.GoToPrevCommand.CanExecute(null))
255 RegistrationViewModel.GoToPrevCommand.Execute(null);
256
257 return true;
258 }
259 else
260 {
261 MainThread.BeginInvokeOnMainThread(async () =>
262 {
263 if (ViewModel is not null)
264 await ViewModel.GoBack();
265 else
266 await ServiceRef.UiService.GoBackAsync();
267 });
268
269 return true;
270 }
271 }
272 catch
273 {
274 return base.OnBackButtonPressed();
275 }
276 }
277
287 public virtual bool OnToolbarBackButtonPressed()
288 {
289 return this.OnBackButtonPressed();
290 }
291
295 protected sealed override async void OnParentSet()
296 {
297 try
298 {
299 base.OnParentSet();
300
301 BaseViewModel ViewModel = this.ViewModel<BaseViewModel>();
302
303 if (this.Parent is null)
304 {
305 if (ViewModel is ILifeCycleView LifeCycleView)
306 await LifeCycleView.DoDispose();
307 }
308 else
309 {
310 if (ViewModel is ILifeCycleView LifeCycleView)
311 await LifeCycleView.DoInitialize();
312 }
313 }
314 catch (Exception ex)
315 {
316 Log.Exception(ex);
317 }
318 }
319
320 }
321}
Base class that references services in the app.
Definition: ServiceRef.cs:31
static ILogService LogService
Log service.
Definition: ServiceRef.cs:91
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:55
static IStringLocalizer Localizer
Localization service
Definition: ServiceRef.cs:235
static ISettingsService SettingsService
Settings service.
Definition: ServiceRef.cs:175
Static class that gives access to app-specific themed colors
Definition: AppColors.cs:7
static Color PrimaryBackground
Primary background color.
Definition: AppColors.cs:82
virtual async Task OnAppearingAsync()
Asynchronous OnAppearing-method.
virtual bool OnToolbarBackButtonPressed()
A method which is called when the user presses the back button in the application toolbar at the top ...
EventHandlerAsync? OnAfterAppearing
Event raised after page appears
sealed override async void OnAppearing()
virtual void OnLoaded()
Method called when page has been loaded.
sealed override async void OnParentSet()
Called when the Page's Element.Parent property has changed.
T ViewModel< T >()
Convenience function for accessing the BindableObject.BindingContext property as a view model.
virtual async Task OnDisappearingAsync()
Asynchronous OnAppearing-method.
virtual ? string UniqueId
Gets or sets a unique identifier, which allows this page to be distinguished from other pages of the ...
sealed override bool OnBackButtonPressed()
Overrides the back button behavior to handle navigation internally instead.
sealed override async void OnDisappearing()
EventHandlerAsync? OnBeforeAppearing
Event raised before page appears
BaseViewModel ContentPageModel
Convenience property for accessing the BindableObject.BindingContext property as a view model.
BaseContentPage()
Creates an instance of the BaseContentPage class.
A base class for all view models, inheriting from the BindableObject. NOTE: using this class requir...
virtual async Task DoAppearing()
Method called when view is appearing on the screen.
bool IsAppearing
Returns true if the view model is shown.
virtual async Task GoBack()
Method called when user wants to navigate to the previous screen.
async Task DoDisappearing()
Method called when view is disappearing from the screen.
async Task SaveState()
Called by the parent page when it disappears on screen, before the DoDisappearing method is called.
async Task RestoreState()
Called by the parent page when it appears on screen, after the DoAppearing method is called.
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1647
static Exception UnnestException(Exception Exception)
Unnests an exception, to extract the relevant inner exception.
Definition: Log.cs:818
Interface for views who need to react to life-cycle events.
delegate Task EventHandlerAsync(object Sender, EventArgs e)
Asynchronous version of EventArgs.