App.xaml.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. string license = "vRK+dzgqDsImQkoKZb2i2WhhdsKYquZ5JJUUuOF6q4xySFETAYvInXOu/f3JAvR9X14z/s4W20EE9WSKsbEum6IvdY8uLoNFy6YM7SyvNC0zVPs5T7GnEBaTQ+qVgnGeV88LA7nbZjPGfQmC75jFLlItpetrbbp75d7LQs6ftq/I8akPBz4Kxc3SSow2MFj5GzH6VbCFYSlUKw/TwA4adwM8uk2g6Kh86i23vTzGejQ4FtQ6stKHXn4HllO0bXpWnFihljcZ3R8PR31pFNwT8UUw4h9NVdFcZxpnGRDbwx9QP9WyHpHZLwbeO48ufCgFCVysJEk1riNSTGTAaAP3FUtTVN6c+2dRhnnKQ9BD2+ZXPq1h0mtlp1NQ+RMIko/jptqMsDODbroq9eCcyCqv15famjsc5QhApxJ66Uir6JIEWg+1gHSh2bjFiiXJAZ6NYxZRbQCMGNWAvkvPL3VOmCcPKDpJojB4dAuUzkjcfNP3FtGWASLlf1sxBLPPUH3/SUjuKo61mV+inIkdPNQcpTuQO57aUzB8KSNTD9t5EApfDx1B3KqboczEI8JHpWmS+IJqLCfsdlZLlGqIVobinsWoWlrK+RCjMLVb1nG6cwgJkaUZSOX6lhqKCVjsk/y/UkMufir2Jr3VOcSskcB2q89CFCFifHHHnhIhHClAv/cmsbjAv8udd4vC2c8/IMyeZDTKE61QPTGv5Web7Veww7UvwzOJJzdEyurazXA+8W54OEFOcXWYSdIgqS/vfVtnuO1kXkGQlSX1V0ead/9e5cgbJBA0v6oCmcgFsdJ19cJWN2eb8XRaylJBvRjCLTfxyhbz5ot00OuvKj41xr9kzFxfdbfnnZIUvdt0DhdKOcq/XESwVIL+pht7hkwzW7dk3HovfUURbW/42AUBTS3P/UNn7rp3eHOhZnTYo6erdzuLfnoMUa8M8nhOhIEkkRTxyCgKfwd4akkqZVJx9r83Hlf9ceP1IoDYgrXYwpetLJ8=";
  84. LicenseErrorCode verifyResult = CPDFSDKVerifier.LicenseVerify(license, false);
  85. return (verifyResult == LicenseErrorCode.E_LICENSE_SUCCESS);
  86. }
  87. private void HistoryFile(string item)
  88. {
  89. PDFFileInfo fileInfo = new PDFFileInfo();
  90. fileInfo.FilePath = item;
  91. fileInfo.FileName = Path.GetFileName(item);
  92. fileInfo.FileSize = CommonHelper.GetFileSize(fileInfo.FilePath);
  93. fileInfo.OpenDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  94. FileHistoryHelper<PDFFileInfo>.Instance.AddHistory(fileInfo);
  95. FileHistoryHelper<PDFFileInfo>.Instance.SaveHistory();
  96. }
  97. }
  98. public class FilePathList : List<string>
  99. {
  100. public new void Add(string item)
  101. {
  102. base.Add(item);
  103. }
  104. }
  105. public class ResourceConverter : IValueConverter
  106. {
  107. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  108. {
  109. if (parameter == null || string.IsNullOrEmpty(parameter.ToString()))
  110. {
  111. return string.Empty;
  112. }
  113. return App.MainResourceManager.GetString(parameter.ToString());
  114. }
  115. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  116. {
  117. throw new NotSupportedException();
  118. }
  119. }
  120. }