Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MauiProgram.cs
1using CommunityToolkit.Maui;
2using CommunityToolkit.Maui.Markup;
3using Microsoft.Extensions.Logging;
4using Microsoft.Maui.Controls.Handlers.Items;
5using Microsoft.Maui.Handlers;
6using Microsoft.Maui.LifecycleEvents;
12using ZXing.Net.Maui.Controls;
13using Microsoft.Maui.Platform;
14using SkiaSharp.Views.Maui.Controls.Hosting;
15using SkiaSharp.Views.Maui.Controls;
22using SkiaSharp.Views.Maui.Handlers;
23
24
25
26#if DEBUG
27using DotNet.Meteor.HotReload.Plugin;
28#endif
29#if ANDROID
30using Microsoft.Maui.Controls.Compatibility.Platform.Android;
31#endif
32#if IOS
33using UIKit;
34#endif
35#if WINDOWS
36using Microsoft.UI.Xaml;
37using Microsoft.UI.Xaml.Controls;
38using Microsoft.UI.Xaml.Media;
39using WinScrollBarVisibility = Microsoft.UI.Xaml.Controls.ScrollBarVisibility;
40using WinThickness = Microsoft.UI.Xaml.Thickness;
41using WinSolidColorBrush = Microsoft.UI.Xaml.Media.SolidColorBrush;
42#endif
43
44namespace NeuroAccessMaui
45{
46 public static class MauiProgram
47 {
48 private static MauiApp? instance;
49
50 public static MauiApp CreateMauiApp()
51 {
52 InitMauiControlsHandlers();
53
54 MauiAppBuilder Builder = MauiApp.CreateBuilder();
55
56 Builder.UseMauiApp<App>();
57
58 Builder.ConfigureMauiHandlers(handlers =>
59 {
60 #if IOS || MACCATALYST
61 handlers.AddHandler<Microsoft.Maui.Controls.CollectionView, Microsoft.Maui.Controls.Handlers.Items2.CollectionViewHandler2>();
62 handlers.AddHandler<Microsoft.Maui.Controls.CarouselView, Microsoft.Maui.Controls.Handlers.Items2.CarouselViewHandler2>();
63 #endif
64 handlers.AddHandler<AutoHeightSKCanvasView, SKCanvasViewHandler>();
65 handlers.AddHandler(typeof(AspectRatioLayout), typeof(LayoutHandler));
66 });
67
68 Builder.ConfigureLifecycleEvents(lifecycle =>
69 {
70#if ANDROID
71 lifecycle.AddAndroid(android =>
72 android.OnResume(activity =>
73 {
74 // App has resumed on Android
75 App.RaiseAppActivated();
76 }));
77#elif IOS
78 lifecycle.AddiOS(ios =>
79 ios.OnActivated(app =>
80 {
81 // App has resumed on iOS
82 App.RaiseAppActivated();
83 }));
84#elif WINDOWS
85 lifecycle.AddWindows(windows =>
86 windows.OnActivated((window, args) =>
87 {
88 // App has resumed on Windows
89 App.RaiseAppActivated();
90 }));
91#endif
92 });
93
94 Builder.UseSkiaSharp();
95 Builder.RegisterFirebaseServices();
96#if DEBUG
97 Builder.EnableHotReload();
98 Builder.Logging.AddDebug();
99#endif
100
101 Builder.ConfigureFonts(fonts =>
102 {
103 fonts.AddFont("SpaceGrotesk-Bold.ttf", "SpaceGroteskBold");
104 fonts.AddFont("SpaceGrotesk-SemiBold.ttf", "SpaceGroteskSemiBold");
105 fonts.AddFont("SpaceGrotesk-Medium.ttf", "SpaceGroteskMedium");
106 fonts.AddFont("SpaceGrotesk-Regular.ttf", "SpaceGroteskRegular");
107 fonts.AddFont("NHaasGroteskTXPro-75Bd.ttf", "HaasGroteskBold");
108 fonts.AddFont("NHaasGroteskTXPro-55Rg.ttf", "HaasGroteskRegular");
109 });
110
111 // NuGets
112#if DEBUG
113 Builder.UseMauiCommunityToolkit();
114#else
115 Builder.UseMauiCommunityToolkit(Options =>
116 {
117 Options.SetShouldSuppressExceptionsInAnimations(true);
118 Options.SetShouldSuppressExceptionsInBehaviors(true);
119 Options.SetShouldSuppressExceptionsInConverters(true);
120 });
121#endif
122 Builder.UseMauiCommunityToolkitMarkup();
123 Builder.UseBarcodeReader();
124 Builder.UseLocalizationManager<AppResources>();
125
126 // Register platform specific implementation
127#if ANDROID || IOS || WINDOWS
128 Builder.Services.AddSingleton<IPlatformSpecific, PlatformSpecific>();
129 Builder.Services.AddSingleton<IKeyboardInsetsService, KeyboardInsetsService>();
130#endif
131
132 Builder.RegisterTypes();
133 Builder.RegisterPages();
134
135 instance = Builder.Build();
136
137 // Setup the service provider
138 ServiceRef.Initialize(new HybridServiceProvider(instance.Services));
139
140 return instance;
141 }
142
146 public static MauiApp? Current => instance;
147
148 private static void InitMauiControlsHandlers()
149 {
150 // Layouts (Grid, StackLayout, etc.)
151 LayoutHandler.Mapper.AppendToMapping("GlobalIgnoreSafeArea", (handler, view) =>
152 {
153 // Sets once at handler creation
154 if(view is Microsoft.Maui.Controls.Layout layout)
155 layout.IgnoreSafeArea = true;
156 });
157
158#if IOS
159 ScrollViewHandler.Mapper.AppendToMapping("BouncesScrollViewHandler", (handler, view) =>
160 {
161 if (handler.PlatformView is UIScrollView scroll)
162 {
163 scroll.Bounces = false;
164 }
165 });
166
167 CollectionViewHandler.Mapper.AppendToMapping("BouncesCollectionViewHandler", (handler, view) =>
168 {
169 if (handler.PlatformView is UIScrollView scroll)
170 {
171 scroll.Bounces = false;
172 }
173 });
174
175 Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("BorderStyleEntryHandler", (handler, view) =>
176 {
177 handler.PlatformView.BorderStyle = UIKit.UITextBorderStyle.None;
178 });
179
180 EditorHandler.Mapper.AppendToMapping("BorderlessEditorHandler", (handler, view) =>
181 {
182 handler.PlatformView.BackgroundColor = UIKit.UIColor.Clear;
183 });
184
185 PickerHandler.Mapper.AppendToMapping("BorderlessPickerHandler", (handler, view) =>
186 {
187 handler.PlatformView.BackgroundColor = UIKit.UIColor.Clear;
188 });
189
190 DatePickerHandler.Mapper.AppendToMapping("BorderlessDatePickerHandler", (handler, view) =>
191 {
192 handler.PlatformView.BorderStyle = UIKit.UITextBorderStyle.None;
193 handler.PlatformView.BackgroundColor = UIKit.UIColor.Clear;
194 });
195
196 TimePickerHandler.Mapper.AppendToMapping("BorderlessTimePickerHandler", (handler, view) =>
197 {
198 handler.PlatformView.BorderStyle = UIKit.UITextBorderStyle.None;
199 handler.PlatformView.BackgroundColor = UIKit.UIColor.Clear;
200 });
201#elif ANDROID
202 ScrollViewHandler.Mapper.AppendToMapping("OverScrollModeScrollViewHandler", (handler, view) =>
203 {
204 handler.PlatformView.OverScrollMode = Android.Views.OverScrollMode.Never;
205 });
206
207 CollectionViewHandler.Mapper.AppendToMapping("OverScrollModeCollectionViewHandler", (handler, view) =>
208 {
209 handler.PlatformView.OverScrollMode = Android.Views.OverScrollMode.Never;
210 });
211
212 EntryHandler.Mapper.AppendToMapping("NoUnderlineEntryHandler", (handler, view) =>
213 {
214 handler.PlatformView.BackgroundTintList = Android.Content.Res.ColorStateList.ValueOf(Colors.Transparent.ToAndroid());
215 });
216 PickerHandler.Mapper.AppendToMapping("NoUnderlinePickerHandler", (handler, view) =>
217 {
218 handler.PlatformView.BackgroundTintList = Android.Content.Res.ColorStateList.ValueOf(Colors.Transparent.ToAndroid());
219 });
220 TimePickerHandler.Mapper.AppendToMapping("NoUnderlineTimePickerHandler", (handler, view) =>
221 {
222 handler.PlatformView.BackgroundTintList = Android.Content.Res.ColorStateList.ValueOf(Colors.Transparent.ToAndroid());
223 });
224 DatePickerHandler.Mapper.AppendToMapping("NoUnderlineDatePickerHandler", (handler, view) =>
225 {
226 handler.PlatformView.BackgroundTintList = Android.Content.Res.ColorStateList.ValueOf(Colors.Transparent.ToAndroid());
227 });
228 ImageHandler.Mapper.PrependToMapping(nameof(Microsoft.Maui.IImage.Source), (handler, view) =>
229 {
230 handler.PlatformView?.Clear();
231 });
232#elif WINDOWS
233 ScrollViewHandler.Mapper.AppendToMapping("HideScrollBarsScrollViewHandler", (handler, view) =>
234 {
235 if (handler.PlatformView is ScrollViewer ScrollViewer)
236 {
237 ScrollViewer.VerticalScrollBarVisibility = WinScrollBarVisibility.Hidden;
238 ScrollViewer.HorizontalScrollBarVisibility = WinScrollBarVisibility.Hidden;
239 }
240 });
241
242 CollectionViewHandler.Mapper.AppendToMapping("HideScrollBarsCollectionViewHandler", (handler, view) =>
243 {
244 if (handler.PlatformView is ListViewBase ListViewBase)
245 {
246 ScrollViewer? NestedScrollViewer = FindDescendantScrollViewer(ListViewBase);
247 if (NestedScrollViewer is not null)
248 {
249 NestedScrollViewer.VerticalScrollBarVisibility = WinScrollBarVisibility.Hidden;
250 NestedScrollViewer.HorizontalScrollBarVisibility = WinScrollBarVisibility.Hidden;
251 }
252 }
253 });
254
255 EntryHandler.Mapper.AppendToMapping("NoBorderEntryHandler", (handler, view) =>
256 {
257 if (handler.PlatformView is TextBox TextBox)
258 {
259 TextBox.BorderThickness = new WinThickness(0);
260 TextBox.BorderBrush = new WinSolidColorBrush(Microsoft.UI.Colors.Transparent);
261 TextBox.Background = new WinSolidColorBrush(Microsoft.UI.Colors.Transparent);
262 TextBox.GotFocus += (sender, args) =>
263 {
264 TextBox.BorderThickness = new WinThickness(0);
265 TextBox.BorderBrush = new WinSolidColorBrush(Microsoft.UI.Colors.Transparent);
266 };
267 TextBox.Loaded += (sender, args) =>
268 {
269 TextBox.BorderThickness = new WinThickness(0);
270 TextBox.BorderBrush = new WinSolidColorBrush(Microsoft.UI.Colors.Transparent);
271 };
272 }
273 });
274
275 EditorHandler.Mapper.AppendToMapping("NoBorderEditorHandler", (handler, view) =>
276 {
277 if (handler.PlatformView is TextBox TextBox)
278 {
279 TextBox.BorderThickness = new WinThickness(0);
280 TextBox.BorderBrush = new WinSolidColorBrush(Microsoft.UI.Colors.Transparent);
281 TextBox.Background = new WinSolidColorBrush(Microsoft.UI.Colors.Transparent);
282 TextBox.GotFocus += (sender, args) =>
283 {
284 TextBox.BorderThickness = new WinThickness(0);
285 TextBox.BorderBrush = new WinSolidColorBrush(Microsoft.UI.Colors.Transparent);
286 };
287 TextBox.Loaded += (sender, args) =>
288 {
289 TextBox.BorderThickness = new WinThickness(0);
290 TextBox.BorderBrush = new WinSolidColorBrush(Microsoft.UI.Colors.Transparent);
291 };
292 }
293 });
294
295 PickerHandler.Mapper.AppendToMapping("NoBorderPickerHandler", (handler, view) =>
296 {
297 if (handler.PlatformView is ComboBox ComboBox)
298 {
299 ComboBox.BorderThickness = new WinThickness(0);
300 ComboBox.BorderBrush = new WinSolidColorBrush(Microsoft.UI.Colors.Transparent);
301 ComboBox.Background = new WinSolidColorBrush(Microsoft.UI.Colors.Transparent);
302 ComboBox.GotFocus += (sender, args) =>
303 {
304 ComboBox.BorderThickness = new WinThickness(0);
305 ComboBox.BorderBrush = new WinSolidColorBrush(Microsoft.UI.Colors.Transparent);
306 };
307 ComboBox.Loaded += (sender, args) =>
308 {
309 ComboBox.BorderThickness = new WinThickness(0);
310 ComboBox.BorderBrush = new WinSolidColorBrush(Microsoft.UI.Colors.Transparent);
311 };
312 }
313 });
314
315 DatePickerHandler.Mapper.AppendToMapping("NoBorderDatePickerHandler", (handler, view) =>
316 {
317 if (handler.PlatformView is CalendarDatePicker CalendarDatePicker)
318 {
319 CalendarDatePicker.BorderThickness = new WinThickness(0);
320 CalendarDatePicker.BorderBrush = new WinSolidColorBrush(Microsoft.UI.Colors.Transparent);
321 CalendarDatePicker.Background = new WinSolidColorBrush(Microsoft.UI.Colors.Transparent);
322 CalendarDatePicker.GotFocus += (sender, args) =>
323 {
324 CalendarDatePicker.BorderThickness = new WinThickness(0);
325 CalendarDatePicker.BorderBrush = new WinSolidColorBrush(Microsoft.UI.Colors.Transparent);
326 };
327 CalendarDatePicker.Loaded += (sender, args) =>
328 {
329 CalendarDatePicker.BorderThickness = new WinThickness(0);
330 CalendarDatePicker.BorderBrush = new WinSolidColorBrush(Microsoft.UI.Colors.Transparent);
331 };
332 }
333 });
334 // Windows TimePicker styling skipped (native control lacks BorderThickness). Add custom template if needed.
335#endif
336 }
337
338#if WINDOWS
339 private static ScrollViewer? FindDescendantScrollViewer(DependencyObject Root)
340 {
341 int Count = VisualTreeHelper.GetChildrenCount(Root);
342 for (int i = 0; i < Count; i++)
343 {
344 DependencyObject Child = VisualTreeHelper.GetChild(Root, i);
345 if (Child is ScrollViewer Found)
346 {
347 return Found;
348 }
349
350 ScrollViewer? Nested = FindDescendantScrollViewer(Child);
351 if (Nested is not null)
352 {
353 return Nested;
354 }
355 }
356
357 return null;
358 }
359#endif
360 }
361}
Represents an instance of the Neuro-Access app.
Definition: App.xaml.cs:125
static ? MauiApp Current
Current MAUI app instance.
Definition: MauiProgram.cs:146
A strongly-typed resource class, for looking up localized strings, etc.
Android implementation of platform-specific features.
Base class that references services in the app.
Definition: ServiceRef.cs:43
static void Initialize(IServiceProvider provider)
Initializes the service reference cache with a fresh provider instance. This method must be called wh...
Definition: ServiceRef.cs:86
Default implementation of IKeyboardInsetsService that normalizes platform keyboard measurements.
Interface for platform-specific functions.
Provides normalized keyboard inset information for UI components.