CompressProgressBarDialogViewModel.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using PDF_Office.Model.Dialog.ToolsDialogs;
  2. using ComPDFKit.PDFDocument;
  3. using PDF_Office.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. namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.CompressDialogs
  13. {
  14. public class CompresProgressBarDialogViewModel : BindableBase, IDialogAware
  15. {
  16. #region 参数和属性
  17. private IntPtr compressingIntpr = IntPtr.Zero;
  18. private CPDFDocument.GetPageIndexDelegate indexDelegate = null;
  19. private CPDFDocument tempDocument;
  20. private string _pageIndex = "0";
  21. /// <value>
  22. /// 当前页
  23. /// </value>
  24. public string PageIndex
  25. {
  26. get { return _pageIndex; }
  27. set
  28. {
  29. SetProperty(ref _pageIndex, value);
  30. }
  31. }
  32. private string _pageNumber = "";
  33. /// <value>
  34. /// 总页数
  35. /// </value>
  36. public string PageNumber
  37. {
  38. get { return _pageNumber; }
  39. set
  40. {
  41. SetProperty(ref _pageNumber, value);
  42. }
  43. }
  44. #endregion
  45. #region 逻辑函数
  46. private int GetIndex(int pageindex)
  47. {
  48. PageIndex = pageindex.ToString();
  49. return 0;
  50. }
  51. #endregion
  52. #region 框架行为
  53. public string Title => "";
  54. public event Action<IDialogResult> RequestClose;
  55. public bool CanCloseDialog()
  56. {
  57. return true;
  58. }
  59. public void OnDialogClosed()
  60. {
  61. tempDocument.CompressFile_Cancel(compressingIntpr);
  62. }
  63. public async void OnDialogOpened(IDialogParameters parameters)
  64. {
  65. CPDFDocument doc = null;
  66. string filepath = "";
  67. int compressquality = 45;
  68. parameters.TryGetValue<CPDFDocument>(ParameterNames.PDFDocument, out doc);
  69. parameters.TryGetValue<string>(ParameterNames.FilePath, out filepath);
  70. parameters.TryGetValue<int>("compressDialogModel.CompressQuality", out compressquality);
  71. if (doc != null)
  72. {
  73. PageNumber = doc.PageCount.ToString();
  74. CPDFDocument document = CPDFDocument.InitWithFilePath(doc.FilePath);
  75. indexDelegate += GetIndex;
  76. compressingIntpr = document.CompressFile_Init(compressquality, indexDelegate);
  77. //GC.KeepAlive(indexDelegate);
  78. tempDocument = document;
  79. Trace.WriteLine("compressDialogModel.CompressQuality" + compressquality);
  80. await Task.Run<bool>(() => { return document.CompressFile_Start(compressingIntpr, filepath); });
  81. RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  82. document.Release();
  83. }
  84. }
  85. #endregion
  86. }
  87. }