App.axaml.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using Avalonia;
  5. using Avalonia.Controls;
  6. using Avalonia.Controls.ApplicationLifetimes;
  7. using Avalonia.Data.Core.Plugins;
  8. using Avalonia.Markup.Xaml;
  9. using Comal.Classes;
  10. using InABox.Avalonia;
  11. using InABox.Core;
  12. using PRS.Avalonia.Modules;
  13. using GeoFence = Comal.Classes.GeoFence;
  14. namespace PRS.Avalonia;
  15. public class App : Application
  16. {
  17. public static GeoFence? GPS => null;
  18. public override void Initialize()
  19. {
  20. TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
  21. MobileLogging.LogException += MobileLogging_LogException;
  22. CoreUtils.RegisterClasses();
  23. ComalUtils.RegisterClasses();
  24. CoreUtils.RegisterClasses(typeof(App).Assembly);
  25. AvaloniaXamlLoader.Load(this);
  26. }
  27. private void MobileLogging_LogException(Exception ex, string? tag)
  28. {
  29. HandleException(ex);
  30. }
  31. private void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
  32. {
  33. HandleException(e.Exception);
  34. }
  35. public static void HandleException(Exception e)
  36. {
  37. MobileLogging.LogError(CoreUtils.FormatException(e));
  38. }
  39. public override void OnFrameworkInitializationCompleted()
  40. {
  41. if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  42. {
  43. // Avoid duplicate validations from both Avalonia and the CommunityToolkit.
  44. // More info: https://docs.avaloniaui.net/docs/guides/development-guides/data-validation#manage-validationplugins
  45. DisableAvaloniaDataAnnotationValidation();
  46. desktop.MainWindow = new MainWindow
  47. {
  48. DataContext = new MainViewModel(),
  49. Width = 400,
  50. Height = 700,
  51. WindowStartupLocation = WindowStartupLocation.CenterScreen
  52. };
  53. }
  54. else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform)
  55. {
  56. singleViewPlatform.MainView = new MainView
  57. {
  58. DataContext = new MainViewModel()
  59. };
  60. }
  61. base.OnFrameworkInitializationCompleted();
  62. }
  63. private void DisableAvaloniaDataAnnotationValidation()
  64. {
  65. // Get an array of plugins to remove
  66. var dataValidationPluginsToRemove =
  67. BindingPlugins.DataValidators.OfType<DataAnnotationsValidationPlugin>().ToArray();
  68. // remove each entry found
  69. foreach (var plugin in dataValidationPluginsToRemove) BindingPlugins.DataValidators.Remove(plugin);
  70. }
  71. }