FullScreenWindowViewModel.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using ComPDFKitViewer.PdfViewer;
  2. using PDF_Office.Model;
  3. using Prism.Commands;
  4. using Prism.Mvvm;
  5. using Prism.Regions;
  6. using Prism.Services.Dialogs;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. namespace PDF_Office.ViewModels.Dialog
  15. {
  16. public class FullScreenWindowViewModel : BindableBase, IDialogAware
  17. {
  18. public string Title => "";
  19. private Visibility showTip = Visibility.Collapsed;
  20. public Visibility ShowTip
  21. {
  22. get { return showTip; }
  23. set
  24. {
  25. SetProperty(ref showTip, value);
  26. }
  27. }
  28. private CPDFViewer PDFViewer;
  29. public DelegateCommand ExitCommand { get; set; }
  30. public DelegateCommand<object> LoadCommand { get; set; }
  31. public event Action<IDialogResult> RequestClose;
  32. public string RegionName { get; set; }
  33. /// <summary>
  34. /// 鼠标滚轮缩放的缩放值
  35. /// </summary>
  36. private double[] zoomLevel = { 1.00f, 10, 25, 50, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  37. public FullScreenWindowViewModel()
  38. {
  39. ExitCommand = new DelegateCommand(Exit);
  40. LoadCommand = new DelegateCommand<object>(Load);
  41. RegionName = Guid.NewGuid().ToString();
  42. }
  43. private void Exit()
  44. {
  45. RequestClose.Invoke(new DialogResult(ButtonResult.OK));
  46. }
  47. private void Load(object grid)
  48. {
  49. var Grid = grid as Grid;
  50. if(Grid!=null&&PDFViewer!=null)
  51. {
  52. Grid.Children.Insert(0, PDFViewer);
  53. ShowTip = Visibility.Visible;
  54. }
  55. }
  56. public bool CanCloseDialog()
  57. {
  58. return true;
  59. }
  60. public void OnDialogClosed()
  61. {
  62. if(PDFViewer!=null&&PDFViewer.Document!=null)
  63. {
  64. PDFViewer.Document.Release();
  65. }
  66. }
  67. public void OnDialogOpened(IDialogParameters parameters)
  68. {
  69. string filepath;
  70. string password;
  71. parameters.TryGetValue<string>(ParameterNames.FilePath,out filepath);
  72. parameters.TryGetValue<string>(ParameterNames.PassWord,out password);
  73. if (!string.IsNullOrEmpty(filepath))
  74. {
  75. PDFViewer = new CPDFViewer();
  76. PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
  77. PDFViewer.InitDocument(filepath);
  78. if (PDFViewer.Document == null)
  79. {
  80. RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
  81. }
  82. if (PDFViewer.Document.IsLocked && !string.IsNullOrEmpty(password))
  83. {
  84. PDFViewer.Document.UnlockWithPassword(password);
  85. if (PDFViewer.Document.IsLocked)
  86. {
  87. var dialogservice = Prism.Ioc.ContainerLocator.Container.Resolve(typeof(IDialogService)) as IDialogService;
  88. DialogParameters pairs = new DialogParameters();
  89. pairs.Add(ParameterNames.PDFDocument,PDFViewer.Document);
  90. dialogservice.ShowDialog(DialogNames.VerifyPassWordDialog,pairs,e=> {
  91. if(e.Result!=ButtonResult.OK)
  92. {
  93. RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
  94. }
  95. });
  96. }
  97. }
  98. PDFViewer.Load();
  99. PDFViewer.SetMouseMode(MouseModes.Default);
  100. }
  101. }
  102. #region PDFViewer缩放事件
  103. public void PdfViewer_MouseWheelZoomHandler(object sender, bool e)
  104. {
  105. double newZoom = CheckZoomLevel(PDFViewer.ZoomFactor + (e ? 0.01 : -0.01), e);
  106. PDFViewer.Zoom(newZoom);
  107. }
  108. private double CheckZoomLevel(double zoom, bool IsGrowth)
  109. {
  110. double standardZoom = 100;
  111. if (zoom <= 0.01)
  112. {
  113. return 0.01;
  114. }
  115. if (zoom >= 10)
  116. {
  117. return 10;
  118. }
  119. zoom *= 100;
  120. for (int i = 0; i < zoomLevel.Length - 1; i++)
  121. {
  122. if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth)
  123. {
  124. standardZoom = zoomLevel[i + 1];
  125. break;
  126. }
  127. if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth)
  128. {
  129. standardZoom = zoomLevel[i];
  130. break;
  131. }
  132. }
  133. return standardZoom / 100;
  134. }
  135. #endregion
  136. }
  137. }