CompressProgressBarDialogViewModel.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using PDF_Master.Model.Dialog.ToolsDialogs;
  2. using ComPDFKit.PDFDocument;
  3. using PDF_Master.Model;
  4. using Prism.Mvvm;
  5. using Prism.Services.Dialogs;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Diagnostics;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using PDF_Master.Helper;
  13. using Prism.Commands;
  14. using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolTip;
  15. namespace PDF_Master.ViewModels.Dialog.ToolsDialogs.CompressDialogs
  16. {
  17. public class CompresProgressBarDialogViewModel : BindableBase, IDialogAware
  18. {
  19. #region 参数和属性
  20. private IntPtr compressingIntpr = IntPtr.Zero;
  21. private CPDFDocument.GetPageIndexDelegate indexDelegate = null;
  22. private CPDFDocument tempDocument;
  23. private string _pageIndex = "0";
  24. /// <value>
  25. /// 当前页
  26. /// </value>
  27. public string PageIndex
  28. {
  29. get { return _pageIndex; }
  30. set
  31. {
  32. SetProperty(ref _pageIndex, value);
  33. }
  34. }
  35. private string _pageNumber = "";
  36. /// <value>
  37. /// 总页数
  38. /// </value>
  39. public string PageNumber
  40. {
  41. get { return _pageNumber; }
  42. set
  43. {
  44. SetProperty(ref _pageNumber, value);
  45. }
  46. }
  47. #endregion
  48. public DelegateCommand CloseCompressCommand { get; set; }
  49. public CompresProgressBarDialogViewModel() {
  50. CloseCompressCommand = new DelegateCommand(Close);
  51. }
  52. public void Close() {
  53. RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  54. }
  55. #region 逻辑函数
  56. private int GetIndex(int pageindex)
  57. {
  58. PageIndex = pageindex.ToString();
  59. return 0;
  60. }
  61. #endregion
  62. #region 框架行为
  63. public string Title => "";
  64. public event Action<IDialogResult> RequestClose;
  65. public bool CanCloseDialog()
  66. {
  67. return true;
  68. }
  69. public void OnDialogClosed()
  70. {
  71. tempDocument.CompressFile_Cancel(compressingIntpr);
  72. }
  73. public async void OnDialogOpened(IDialogParameters parameters)
  74. {
  75. CPDFDocument doc = null;
  76. string filepath = "";
  77. string password = "";
  78. int compressquality = 45;
  79. parameters.TryGetValue<CPDFDocument>(ParameterNames.PDFDocument, out doc);
  80. parameters.TryGetValue<string>(ParameterNames.FilePath, out filepath);
  81. parameters.TryGetValue<string>(ParameterNames.PassWord, out password);
  82. parameters.TryGetValue<int>("compressDialogModel.CompressQuality", out compressquality);
  83. if (doc != null)
  84. {
  85. PageNumber = doc.PageCount.ToString();
  86. CPDFDocument document = CPDFDocument.InitWithFilePath(doc.FilePath);
  87. document.UnlockWithPassword(password);
  88. indexDelegate += GetIndex;
  89. compressingIntpr = document.CompressFile_Init(compressquality, indexDelegate);
  90. //GC.KeepAlive(indexDelegate);
  91. tempDocument = document;
  92. Trace.WriteLine("compressDialogModel.CompressQuality" + compressquality);
  93. await Task.Run<bool>(() => { return document.CompressFile_Start(compressingIntpr, filepath); });
  94. CommonHelper.ShowFileBrowser(filepath);
  95. RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  96. document.Release();
  97. }
  98. }
  99. #endregion
  100. }
  101. }