FullScreenWindowViewModel.cs 5.0 KB

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