App.xaml.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.Xml;
  15. using ComPDFKit.NativeMethod;
  16. using Compdfkit_Tools.Helper;
  17. using PDFViewer.Properties;
  18. namespace PDFViewer
  19. {
  20. /// <summary>
  21. /// Interaction logic for App.xaml
  22. /// </summary>
  23. public partial class App: Application
  24. {
  25. public static bool DefaultPDFLoaded = false;
  26. public static string CurrentLanguage = Settings.Default.Language;
  27. public static Dictionary<string, string> Languages= new Dictionary<string, string>()
  28. {
  29. {"English","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. protected override void OnStartup(StartupEventArgs e)
  34. {
  35. string currentCulture;
  36. if (string.IsNullOrEmpty(CurrentLanguage))
  37. {
  38. currentCulture = CultureInfo.CurrentCulture.Name;
  39. CurrentLanguage = Languages.FirstOrDefault(x => x.Value == currentCulture).Key;
  40. if (!Languages.ContainsValue(currentCulture))
  41. {
  42. CurrentLanguage = "English";
  43. currentCulture = "en-US";
  44. }
  45. Settings.Default.Language = CurrentLanguage;
  46. Settings.Default.Save();
  47. }
  48. else
  49. {
  50. currentCulture = Languages.TryGetValue(CurrentLanguage, out currentCulture) ? currentCulture : "en-US";
  51. }
  52. Thread.CurrentThread.CurrentUICulture = new CultureInfo(currentCulture);
  53. Thread.CurrentThread.CurrentCulture = new CultureInfo(currentCulture);
  54. base.OnStartup(e);
  55. LicenseVerify();
  56. FileHistoryHelper<PDFFileInfo>.Instance.LoadHistory();
  57. }
  58. private static bool LicenseVerify()
  59. {
  60. if (!CPDFSDKVerifier.LoadNativeLibrary())
  61. return false;
  62. LicenseErrorCode verifyResult = CPDFSDKVerifier.LicenseVerify("license_key_windows.txt", true);
  63. return (verifyResult != LicenseErrorCode.E_LICENSE_SUCCESS);
  64. }
  65. }
  66. public class FilePathList : List<string>
  67. {
  68. public new void Add(string item)
  69. {
  70. base.Add(item);
  71. HistoryFile(item);
  72. }
  73. private void HistoryFile(string item)
  74. {
  75. PDFFileInfo fileInfo = new PDFFileInfo();
  76. fileInfo.FilePath = item;
  77. fileInfo.FileName = Path.GetFileName(item);
  78. fileInfo.FileSize = CommonHelper.GetFileSize(fileInfo.FilePath);
  79. fileInfo.OpenDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  80. FileHistoryHelper<PDFFileInfo>.Instance.AddHistory(fileInfo);
  81. FileHistoryHelper<PDFFileInfo>.Instance.SaveHistory();
  82. }
  83. }
  84. public class ResourceConverter : IValueConverter
  85. {
  86. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  87. {
  88. if (parameter == null || string.IsNullOrEmpty(parameter.ToString()))
  89. {
  90. return string.Empty;
  91. }
  92. return App.MainResourceManager.GetString(parameter.ToString());
  93. }
  94. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  95. {
  96. throw new NotSupportedException();
  97. }
  98. }
  99. }