ToolsProgressBarDialogViewModel.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using ComPDFKit.PDFDocument;
  2. using PDF_Office.Model;
  3. using PDF_Office.Model.Dialog.ToolsDialogs;
  4. using Prism.Commands;
  5. using Prism.Mvvm;
  6. using Prism.Services.Dialogs;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.Linq;
  11. using System.Threading.Tasks;
  12. using System.Xml.Linq;
  13. using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
  14. namespace PDF_Office.ViewModels.Dialog.ToolsDialogs
  15. {
  16. public class ToolsProgressBarDialogViewModel : BindableBase, IDialogAware
  17. {
  18. private IntPtr compressingIntpr = IntPtr.Zero;
  19. private CPDFDocument.GetPageIndexDelegate indexDelegate = null;
  20. private CPDFDocument tempDocument;
  21. private string _pageIndex = "0";
  22. /// <summary>
  23. /// 页面尺寸
  24. /// </summary>
  25. public string PageIndex
  26. {
  27. get { return _pageIndex; }
  28. set
  29. {
  30. SetProperty(ref _pageIndex, value);
  31. }
  32. }
  33. private string _pageNumber = "";
  34. /// <summary>
  35. /// 页面尺寸
  36. /// </summary>
  37. public string PageNumber
  38. {
  39. get { return _pageNumber; }
  40. set
  41. {
  42. SetProperty(ref _pageNumber, value);
  43. }
  44. }
  45. private int GetIndex(int pageindex)
  46. {
  47. PageIndex= pageindex.ToString();
  48. return 0;
  49. }
  50. public ToolsProgressBarDialogViewModel()
  51. {
  52. }
  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. }
  86. }