App.xaml.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using Prism.DryIoc;
  2. using Prism.Ioc;
  3. using ProjectManagementSystem.Common.Extenions;
  4. using ProjectManagementSystem.Common.Function;
  5. using ProjectManagementSystem.Common.Logger;
  6. using ProjectManagementSystem.Language;
  7. using ProjectManagementSystemView.Infrastructure;
  8. using ProjectManagementSystemView.ViewModels;
  9. using ProjectManagementSystemView.Views;
  10. using ProjectManagementSystemView.Views.Dialogs;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Configuration;
  14. using System.Data;
  15. using System.Linq;
  16. using System.Threading.Tasks;
  17. using System.Windows;
  18. namespace ProjectManagementSystemView
  19. {
  20. /// <summary>
  21. /// App.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class App : PrismApplication
  24. {
  25. private static System.Threading.Mutex mutex;
  26. protected override Window CreateShell()
  27. {
  28. return Container.Resolve<MainView>();
  29. }
  30. protected override void OnStartup(StartupEventArgs e)
  31. {
  32. try
  33. {
  34. var cultureInfo = AppSetting.TryGetValue<string>("CultureInfo");
  35. if (!string.IsNullOrWhiteSpace(cultureInfo))
  36. {
  37. ResourceService.Current.ChangeCulture(cultureInfo);
  38. }
  39. var assemblyName = this.GetType().Assembly.GetName().Name;
  40. mutex = new System.Threading.Mutex(true, $"Global\\{assemblyName}");
  41. if (mutex.WaitOne(0, false))
  42. {
  43. base.OnStartup(e);
  44. DispatcherUnhandledException += App_DispatcherUnhandledException;
  45. TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
  46. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  47. }
  48. else
  49. {
  50. MessageBox.Show(Langs.ProgramIsAlreadyRunning, Langs.Prompt);
  51. this.Shutdown();
  52. }
  53. }
  54. catch (Exception ex)
  55. {
  56. CLog.Instance.SystemLog.WriteExceptionCaller(ex);
  57. MessageBox.Show($"{Langs.ApplicationRunningAbnormally}{ex.Message}");
  58. this.Shutdown();
  59. }
  60. }
  61. private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
  62. {
  63. e.SetObserved();
  64. CLog.Instance.SystemLog.WriteExceptionCaller(e.Exception);
  65. MessageBox.Show($"{Langs.TaskScheduler_UnobservedTaskException}{e.Exception.Message}");
  66. }
  67. private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  68. {
  69. e.Handled = true;
  70. CLog.Instance.SystemLog.WriteExceptionCaller(e.Exception);
  71. MessageBox.Show($"{Langs.App_DispatcherUnhandledException}{e.Exception.Message}");
  72. }
  73. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  74. {
  75. Exception exception = e.ExceptionObject as Exception;
  76. CLog.Instance.SystemLog.WriteExceptionCaller(exception);
  77. MessageBox.Show($"{Langs.CurrentDomain_UnhandledException}{exception.Message}");
  78. }
  79. protected override void RegisterTypes(IContainerRegistry containerRegistry)
  80. {
  81. containerRegistry.Register<IDialogHostService, DialogHostService>();
  82. containerRegistry.RegisterForNavigation<LogView>();
  83. containerRegistry.RegisterForNavigation<CallTaskView>();
  84. containerRegistry.RegisterForNavigation<CurrentTaskView>();
  85. containerRegistry.RegisterForNavigation<DebugAgvView>();
  86. containerRegistry.RegisterForNavigation<DebugView>();
  87. containerRegistry.RegisterForNavigation<DebugPackRgvView>();
  88. containerRegistry.RegisterForNavigation<DebugPackView>();
  89. containerRegistry.RegisterForNavigation<DebugIOView>();
  90. containerRegistry.RegisterForNavigation<DebugPlcView>();
  91. containerRegistry.RegisterForNavigation<DebugRailAgvView>();
  92. containerRegistry.RegisterForNavigation<DebugRollerHxView>();
  93. containerRegistry.RegisterForNavigation<PmsTaskInfoView>();
  94. containerRegistry.RegisterForNavigation<WmsQueryView>();
  95. containerRegistry.RegisterForNavigation<Wms2DView>();
  96. containerRegistry.RegisterForNavigation<Wms3DView>();
  97. containerRegistry.RegisterForNavigation<ModifyWmsView>();
  98. containerRegistry.RegisterForNavigation<SimpleTaskView>();
  99. }
  100. }
  101. }