ViewModelBase.cs 5.3 KB

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