ViewModelBase.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.ApplicationLifetimes;
  4. using Avalonia.Media;
  5. using Avalonia.Threading;
  6. using ReactiveUI;
  7. using ReactiveUI.Validation.Helpers;
  8. using System;
  9. using System.ComponentModel;
  10. using System.IO;
  11. using System.Net;
  12. using VeloeMinecraftLauncher.Utils;
  13. using VeloeMinecraftLauncher.Views;
  14. using VeloeMinecraftLauncher.Views.TitleBar;
  15. namespace VeloeMinecraftLauncher.ViewModels;
  16. public class ViewModelBase : ReactiveValidationObject
  17. {
  18. public ViewModelBase()
  19. {
  20. this.RaisePropertyChanged(nameof(InterfaceColor));
  21. this.RaisePropertyChanged(nameof(MaterialOpacity));
  22. }
  23. public Color InterfaceColor
  24. {
  25. get => Color.Parse("Black");
  26. set => this.RaisePropertyChanged(nameof(InterfaceColor));
  27. }
  28. public float MaterialOpacity
  29. {
  30. get => 0.5F;
  31. set => this.RaisePropertyChanged(nameof(MaterialOpacity));
  32. }
  33. public static void OpenErrorWindow(Exception ex)
  34. {
  35. string message = string.Empty;
  36. string stackTrace = string.Empty;
  37. string logfile = string.Empty;
  38. Exception? innerException;
  39. switch (ex)
  40. {
  41. case WebException or Win32Exception or JavaProcessException or IOException:
  42. message = ex.Message;
  43. Settings.logger.Error(ex.Message);
  44. if (ex is JavaProcessException javaEx && Path.Exists(javaEx.LogPath))
  45. logfile = ((JavaProcessException)ex).LogPath;
  46. if (ex.StackTrace is not null)
  47. Settings.logger.Error(ex.StackTrace);
  48. innerException = ex.InnerException;
  49. while (innerException is not null)
  50. {
  51. Settings.logger.Error(innerException.Message);
  52. if (innerException.StackTrace is not null)
  53. Settings.logger.Error(innerException.StackTrace);
  54. innerException = innerException.InnerException;
  55. }
  56. break;
  57. default:
  58. message = ex.Message;
  59. stackTrace = ex.StackTrace ?? string.Empty;
  60. Settings.logger.Error(ex.Message);
  61. if (ex.StackTrace is not null)
  62. Settings.logger.Error(ex.StackTrace);
  63. innerException = ex.InnerException;
  64. while (innerException is not null)
  65. {
  66. message += "\n" + innerException.Message;
  67. stackTrace += "\n" + innerException.StackTrace;
  68. Settings.logger.Error(innerException.Message);
  69. if (innerException.StackTrace is not null)
  70. Settings.logger.Error(innerException.StackTrace);
  71. innerException = innerException.InnerException;
  72. }
  73. break;
  74. }
  75. OpenErrorWindow(message, logfile, stackTrace);
  76. }
  77. public static void OpenErrorWindow(string message, string logfile = "", string stackTrace = "")
  78. {
  79. #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
  80. if (Application.Current is not null && Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  81. {
  82. Dispatcher.UIThread.InvokeAsync(() =>
  83. {
  84. var ErrorMessageViewModel = new MessageWindowViewModel(message, "Error message", logfile: logfile);
  85. var ErrorMessage = new MessageWindow()
  86. {
  87. DataContext = ErrorMessageViewModel
  88. };
  89. //TODO change it to binding
  90. ErrorMessage.GetControl<TitleBarWindow>("titleBar").TitleText = "Error Message";
  91. ErrorMessage.ShowDialog(desktop.MainWindow);
  92. });
  93. }
  94. #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
  95. }
  96. }