ViewContentViewModel.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using Microsoft.Win32;
  2. using Prism.Commands;
  3. using Prism.Mvvm;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using ComPDFKitViewer.PdfViewer;
  10. using Prism.Regions;
  11. using DryIoc;
  12. using System.Diagnostics;
  13. using Prism.Services.Dialogs;
  14. using PDF_Office.CustomControl;
  15. using PDF_Office.Model;
  16. using PDF_Office.Tools;
  17. namespace PDF_Office.ViewModels
  18. {
  19. public class ViewContentViewModel : BindableBase, INavigationAware
  20. {
  21. public DelegateCommand LoadFile { get; set; }
  22. public DelegateCommand Load { get; set; }
  23. private CPDFViewer PDFViewer { get; set; }
  24. private MainContentViewModel mainViewModel { get; set; }
  25. public IRegionManager region;
  26. public IDialogService dialogs;
  27. public string ViwerRegionName { get; set; }
  28. public string BOTARegionName { get; set; }
  29. public string PropertyRegionName { get; set; }
  30. private bool isOpenFile = false;
  31. /// <summary>
  32. /// 鼠标滚轮缩放的缩放值
  33. /// </summary>
  34. private double[] zoomLevel = { 1.00f, 10, 25, 50, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  35. public ViewContentViewModel(IRegionManager regionManager,IDialogService dialogService)
  36. {
  37. region = regionManager;
  38. dialogs = dialogService;
  39. LoadFile = new DelegateCommand(loadFile);
  40. Load = new DelegateCommand(LoadControl);
  41. ViwerRegionName = Guid.NewGuid().ToString();
  42. BOTARegionName = Guid.NewGuid().ToString();
  43. PropertyRegionName = Guid.NewGuid().ToString();
  44. }
  45. private void LoadControl()
  46. {
  47. //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域
  48. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  49. {
  50. region.RequestNavigate(BOTARegionName, "BOTAContent");
  51. }
  52. ));
  53. }
  54. /// <summary>
  55. /// 将PDFViwer
  56. /// </summary>
  57. private void loadFile()
  58. {
  59. PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
  60. region.AddToRegion(ViwerRegionName, PDFViewer);
  61. }
  62. #region PDFViewer鼠标滚轮缩放事件
  63. public void PdfViewer_MouseWheelZoomHandler(object sender, bool e)
  64. {
  65. double newZoom = CheckZoomLevel(PDFViewer.ZoomFactor + (e ? 0.01 : -0.01), e);
  66. PDFViewer.Zoom(newZoom);
  67. }
  68. private double CheckZoomLevel(double zoom, bool IsGrowth)
  69. {
  70. double standardZoom = 100;
  71. if (zoom <= 0.01)
  72. {
  73. return 0.01;
  74. }
  75. if (zoom >= 10)
  76. {
  77. return 10;
  78. }
  79. zoom *= 100;
  80. for (int i = 0; i < zoomLevel.Length - 1; i++)
  81. {
  82. if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth)
  83. {
  84. standardZoom = zoomLevel[i + 1];
  85. break;
  86. }
  87. if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth)
  88. {
  89. standardZoom = zoomLevel[i];
  90. break;
  91. }
  92. }
  93. return standardZoom / 100;
  94. }
  95. #endregion
  96. public void OnNavigatedTo(NavigationContext navigationContext)
  97. {
  98. if (isOpenFile)
  99. return;
  100. var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel;
  101. if (mainVM != null)
  102. {
  103. mainViewModel = mainVM;
  104. }
  105. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  106. if (pdfview!=null)
  107. {
  108. PDFViewer = pdfview;
  109. loadFile();
  110. }
  111. isOpenFile = true;
  112. }
  113. public bool IsNavigationTarget(NavigationContext navigationContext)
  114. {
  115. return true;
  116. }
  117. public void OnNavigatedFrom(NavigationContext navigationContext)
  118. {
  119. }
  120. }
  121. }