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;
7using System.Reflection;
8
10{
14 public abstract class BaseContentPage : ContentView, ILifeCycleView
15 {
19 public T ViewModel<T>() where T : BaseViewModel
20 {
21 if (BindingContext is T vm)
22 return vm;
23 throw new InvalidOperationException($"Expected BindingContext of type {typeof(T).Name}, but got {BindingContext?.GetType().Name ?? "null"}");
24 }
25
30 {
31 get => this.ViewModel<BaseViewModel>();
32 set => this.BindingContext = value;
33 }
34
38 public virtual string? UniqueId { get; set; }
39
43 public bool IsInitialized { get; private set; }
44
48 public bool IsAppearing { get; private set; }
49
53 protected BaseContentPage()
54 {
55 this.SetDynamicResource(Microsoft.Maui.Controls.VisualElement.BackgroundColorProperty, "SurfaceBackgroundWL");
56 }
57
62 public virtual async Task OnInitializeAsync()
63 {
64 await App.ServicesReady;
65 // Forward to ViewModel if it implements ILifeCycleView
66 if (BindingContext is ILifeCycleView vm)
67 await vm.OnInitializeAsync();
68 }
69
73 public virtual async Task OnDisposeAsync()
74 {
75 if (BindingContext is ILifeCycleView vm)
76 await vm.OnDisposeAsync();
77 }
78
82 public virtual async Task OnAppearingAsync()
83 {
84 try
85 {
86 await App.ServicesReady;
87
88 if (BindingContext is BaseViewModel vm)
89 {
90 await vm.OnAppearingAsync();
91 if (await ServiceRef.SettingsService.WaitInitDone())
92 await vm.RestoreState();
93 }
94
95 // Events (optional)
96 if (OnBeforeAppearing is not null)
97 await OnBeforeAppearing(this, EventArgs.Empty);
98 }
99 catch (Exception ex)
100 {
101 ServiceRef.LogService.LogException(ex);
102 }
103 }
104
108 public virtual async Task OnDisappearingAsync()
109 {
110
111 try
112 {
113 if (BindingContext is BaseViewModel vm)
114 {
115 if (await ServiceRef.SettingsService.WaitInitDone())
116 await vm.SaveState();
117
118 await vm.OnDisappearingAsync();
119 }
120
121 // Events (optional)
122 if (OnAfterDisappearing is not null)
123 await OnAfterDisappearing(this, EventArgs.Empty);
124 }
125 catch (Exception ex)
126 {
127 ServiceRef.LogService.LogException(ex);
128 }
129 }
130
135
140
141
142 }
143}
Represents an instance of the Neuro-Access app.
Definition: App.xaml.cs:125
static Task ServicesReady
Task that completes once all core services have finished loading.
Definition: App.xaml.cs:174
Base class that references services in the app.
Definition: ServiceRef.cs:43
static ILogService LogService
Log service.
Definition: ServiceRef.cs:214
static ISettingsService SettingsService
Settings service.
Definition: ServiceRef.cs:298
A base class for all pages, intended for custom navigation with explicit life-cycle events.
virtual async Task OnAppearingAsync()
Called when the page is about to be shown. Triggers restore state, appearing logic,...
virtual async Task OnInitializeAsync()
Override to register event handlers, process navigation args, etc. Called ONCE per page lifetime.
bool IsInitialized
True if this page is initialized (see ILifeCycleView).
bool IsAppearing
True if this page is currently visible/appearing (see ILifeCycleView).
EventHandlerAsync? OnAfterDisappearing
Event raised after page disappears.
T ViewModel< T >()
Access the page's strongly-typed view model.
virtual async Task OnDisappearingAsync()
Called when the page is about to be hidden. Triggers save state, disappearing logic,...
virtual ? string UniqueId
Gets or sets a unique identifier for distinguishing this page from others of the same type.
virtual async Task OnDisposeAsync()
Override to unregister event handlers, cleanup, etc. Called when page is permanently removed.
EventHandlerAsync? OnBeforeAppearing
Event raised before page appears.
BaseViewModel ContentPageModel
Convenience property for accessing the BindingContext as a BaseViewModel.
BaseContentPage()
Initializes the page with platform safe-area and default background.
A base class for all view models, inheriting from the BindableObject. NOTE: using this class requir...
Interface for views who need to react to life-cycle events.
delegate Task EventHandlerAsync(object Sender, EventArgs e)
Asynchronous version of EventArgs.