App.xaml.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Resources;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Data;
  14. using System.Windows.Threading;
  15. using System.Xml;
  16. using ComPDFKit.NativeMethod;
  17. using Compdfkit_Tools.Helper;
  18. using PDFViewer.Properties;
  19. namespace PDFViewer
  20. {
  21. /// <summary>
  22. /// Interaction logic for App.xaml
  23. /// </summary>
  24. public partial class App: Application
  25. {
  26. public static string CurrentCulture = Settings.Default.Cultrue;
  27. public static List<string> SupportedCultureList= new List<string>()
  28. {
  29. "en-US", "zh-CN"
  30. };
  31. public static FilePathList OpenedFilePathList = new FilePathList();
  32. public static ResourceManager MainResourceManager = new ResourceManager("PDFViewer.Strings.SettingDialog", Assembly.GetExecutingAssembly());
  33. public App()
  34. {
  35. this.DispatcherUnhandledException += App_DispatcherUnhandledException;
  36. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  37. TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
  38. }
  39. private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
  40. {
  41. var ex = e.Exception as Exception;
  42. if (ex != null)
  43. {
  44. MessageBox.Show("发生Task未处理异常:" + ex.Message + "\n" + ex.StackTrace, "系统错误", MessageBoxButton.OK, MessageBoxImage.Error);
  45. }
  46. }
  47. private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  48. {
  49. if (e.ExceptionObject is System.Exception)
  50. {
  51. Exception ex = (System.Exception)e.ExceptionObject;
  52. MessageBox.Show("发生非UI线程异常:" + ex.Message + "\n" + ex.StackTrace, "系统错误", MessageBoxButton.OK, MessageBoxImage.Error);
  53. }
  54. }
  55. private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
  56. {
  57. e.Handled = true;
  58. MessageBox.Show("发生UI线程异常:" + e.Exception.Message + "\n" + e.Exception.StackTrace, "系统错误", MessageBoxButton.OK, MessageBoxImage.Error);
  59. }
  60. protected override void OnStartup(StartupEventArgs e)
  61. {
  62. if (string.IsNullOrEmpty(CurrentCulture))
  63. {
  64. CurrentCulture = CultureInfo.CurrentCulture.Name;
  65. if (!SupportedCultureList.Contains(CurrentCulture))
  66. {
  67. CurrentCulture = "en-US";
  68. }
  69. Settings.Default.Cultrue = CurrentCulture;
  70. Settings.Default.Save();
  71. }
  72. Thread.CurrentThread.CurrentUICulture = new CultureInfo(CurrentCulture);
  73. Thread.CurrentThread.CurrentCulture = new CultureInfo(CurrentCulture);
  74. base.OnStartup(e);
  75. LicenseVerify();
  76. HistoryFile(@"TestFile\ComPDFKit_Sample_File_Windows.pdf");
  77. FileHistoryHelper<PDFFileInfo>.Instance.LoadHistory();
  78. }
  79. private static bool LicenseVerify()
  80. {
  81. if (!CPDFSDKVerifier.LoadNativeLibrary())
  82. return false;
  83. LicenseErrorCode verifyResult = CPDFSDKVerifier.LicenseVerify("license_key_windows.txt", true);
  84. return (verifyResult == LicenseErrorCode.E_LICENSE_SUCCESS);
  85. }
  86. private void HistoryFile(string item)
  87. {
  88. PDFFileInfo fileInfo = new PDFFileInfo();
  89. fileInfo.FilePath = item;
  90. fileInfo.FileName = Path.GetFileName(item);
  91. fileInfo.FileSize = CommonHelper.GetFileSize(fileInfo.FilePath);
  92. fileInfo.OpenDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  93. FileHistoryHelper<PDFFileInfo>.Instance.AddHistory(fileInfo);
  94. FileHistoryHelper<PDFFileInfo>.Instance.SaveHistory();
  95. }
  96. }
  97. public class FilePathList : List<string>
  98. {
  99. public new void Add(string item)
  100. {
  101. base.Add(item);
  102. }
  103. }
  104. public class ResourceConverter : IValueConverter
  105. {
  106. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  107. {
  108. if (parameter == null || string.IsNullOrEmpty(parameter.ToString()))
  109. {
  110. return string.Empty;
  111. }
  112. return App.MainResourceManager.GetString(parameter.ToString());
  113. }
  114. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  115. {
  116. throw new NotSupportedException();
  117. }
  118. }
  119. }