App.xaml.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using MesAdaptor;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. namespace AwInitilizer
  12. {
  13. /// <summary>
  14. /// Interaction logic for App.xaml
  15. /// </summary>
  16. public partial class App : Application
  17. {
  18. public App():base()
  19. {
  20. DispatcherUnhandledException += App_DispatcherUnhandledException;
  21. }
  22. protected override void OnActivated(EventArgs e)
  23. {
  24. base.OnActivated(e);
  25. SetUiLanguage();
  26. SetMes();
  27. var startResult = SajetConnect.SajetTransStart();
  28. }
  29. private void SetUiLanguage()
  30. {
  31. var langSetting = AwInitilizer.Properties.Settings.Default.Language;
  32. if (langSetting.ToLower() == "zh-chs")
  33. {
  34. Cultures.CulturesHelper.Instance.ChangeCulture(System.Globalization.CultureInfo.GetCultureInfo("zh-CHS"));
  35. }
  36. else
  37. {
  38. Cultures.CulturesHelper.Instance.ChangeCulture(System.Globalization.CultureInfo.GetCultureInfo("en-US"));
  39. }
  40. }
  41. private void SetMes()
  42. {
  43. var mesSetting = AwInitilizer.Properties.Settings.Default.MES.ToLower();
  44. if (mesSetting == "php" || mesSetting == "shinewave")
  45. {
  46. SajetConnect.Type = MesType.shinewave;
  47. }
  48. else if (mesSetting == "phv" || mesSetting == "sajet")
  49. {
  50. SajetConnect.Type = MesType.sajet;
  51. }
  52. }
  53. private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  54. {
  55. var fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";
  56. var msg = e.Exception.Message + "\n"+ e.Exception.StackTrace;
  57. File.WriteAllText(fileName,msg);
  58. MessageBox.Show($"App crashed,Please report error and send back {fileName}");
  59. Application.Current.Shutdown();
  60. }
  61. protected override void OnExit(ExitEventArgs e)
  62. {
  63. base.OnExit(e);
  64. var stopResult = SajetConnect.SajetTransClose();
  65. Environment.Exit(0);
  66. }
  67. }
  68. }