ViewContentViewModel.cs 4.2 KB

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