ReadViewContentViewModel.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using ComPDFKitViewer.PdfViewer;
  2. using PDF_Office.Model;
  3. using PDF_Office.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.Drawing;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. namespace PDF_Office.ViewModels.PropertyPanel.ViewModular
  17. {
  18. public class ReadViewContentViewModel : BindableBase, INavigationAware
  19. {
  20. #region 文案
  21. private string T_ESCreadMode;
  22. public string T_ESCReadMode
  23. {
  24. get { return T_ESCreadMode; }
  25. set
  26. {
  27. SetProperty(ref T_ESCreadMode, value);
  28. }
  29. }
  30. private void InitString()
  31. {
  32. T_ESCReadMode = App.MainPageLoader.GetString("GlobalTip_ESCReadMode");
  33. }
  34. #endregion
  35. public string ReadRegionName { get; set; }
  36. public string ReadModeRegionName { get; set; }
  37. public ViewContentViewModel ViewContentViewModel { get; set; }
  38. public CPDFViewer PDFViewer { get; set; }
  39. private Visibility pressEscTip = Visibility.Visible;
  40. public Visibility PressEscTip
  41. {
  42. get { return pressEscTip; }
  43. set
  44. {
  45. SetProperty(ref pressEscTip, value);
  46. }
  47. }
  48. private bool canSave;
  49. /// <summary>
  50. /// 是否可以保存
  51. /// </summary>
  52. public bool CanSave
  53. {
  54. get { return canSave; }
  55. set
  56. {
  57. SetProperty(ref canSave, value);
  58. }
  59. }
  60. private bool canUndo;
  61. /// <summary>
  62. /// 是否可以进行Undo
  63. /// </summary>
  64. public bool CanUndo
  65. {
  66. get { return canUndo; }
  67. set
  68. {
  69. SetProperty(ref canUndo, value);
  70. }
  71. }
  72. private bool canRedo;
  73. /// <summary>
  74. /// 是否可以进行Redo
  75. /// </summary>
  76. public bool CanRedo
  77. {
  78. get { return canRedo; }
  79. set
  80. {
  81. SetProperty(ref canRedo, value);
  82. }
  83. }
  84. private Visibility readModeContentVisibility = Visibility.Collapsed;
  85. public Visibility ReadModeContentVisibility
  86. {
  87. get { return readModeContentVisibility; }
  88. set
  89. {
  90. SetProperty(ref readModeContentVisibility, value);
  91. }
  92. }
  93. private Visibility rectangleReadModeVisibility = Visibility.Collapsed;
  94. public Visibility RectangleReadModeVisibility
  95. {
  96. get { return rectangleReadModeVisibility; }
  97. set
  98. {
  99. SetProperty(ref rectangleReadModeVisibility, value);
  100. }
  101. }
  102. public IRegionManager region;
  103. public IDialogService dialogs;
  104. /// <summary>
  105. /// 鼠标滚轮缩放的缩放值
  106. /// </summary>
  107. private double[] zoomLevel = { 1.00f, 10, 25, 50, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  108. public DelegateCommand Load { get; set; }
  109. public ReadViewContentViewModel(IRegionManager regionManager, IDialogService dialogService)
  110. {
  111. region = regionManager;
  112. dialogs = dialogService;
  113. ReadRegionName = RegionNames.ReadRegionName;
  114. ReadModeRegionName = RegionNames.ReadModePageRegionName;
  115. ReadModeContentVisibility = Visibility.Visible;
  116. RectangleReadModeVisibility = Visibility.Collapsed;
  117. InitString();
  118. }
  119. public bool IsNavigationTarget(NavigationContext navigationContext)
  120. {
  121. return true;
  122. }
  123. public void OnNavigatedFrom(NavigationContext navigationContext)
  124. {
  125. }
  126. public async void OnNavigatedTo(NavigationContext navigationContext)
  127. {
  128. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  129. pdfview.SetBackgroundBrush(new System.Windows.Media.SolidColorBrush(Settings.Default.AppProperties.InitialVIew.BackGroundInFulWindow));
  130. ViewContentViewModel viewContentView = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
  131. if (pdfview != null)
  132. {
  133. PDFViewer = pdfview;
  134. PressEscTip = Visibility.Visible;
  135. LoadFile();
  136. await System.Threading.Tasks.Task.Delay(3000);
  137. PressEscTip = Visibility.Collapsed;
  138. }
  139. if (viewContentView != null)
  140. {
  141. ViewContentViewModel = viewContentView;
  142. }
  143. }
  144. /// <summary>
  145. /// 将PDFViwer添加到Region
  146. /// </summary>
  147. private void LoadFile()
  148. {
  149. PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
  150. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  151. CanSave = PDFViewer.UndoManager.CanSave;
  152. CanUndo = PDFViewer.UndoManager.CanUndo;
  153. CanRedo = PDFViewer.UndoManager.CanRedo;
  154. if (region.Regions.ContainsRegionWithName(ReadRegionName))
  155. {
  156. if (region.Regions[ReadRegionName].Views.Contains(PDFViewer))
  157. {
  158. var contentRegion = region.Regions[ReadRegionName];
  159. contentRegion.Remove(PDFViewer);
  160. }
  161. region.AddToRegion(ReadRegionName, PDFViewer);
  162. }
  163. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  164. {
  165. NavigationParameters parameters = new NavigationParameters();
  166. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  167. parameters.Add(ParameterNames.ViewContentViewModel, this);
  168. region.RequestNavigate(ReadModeRegionName, "ReadModeContent", parameters);
  169. }
  170. ));
  171. }
  172. public void PdfViewer_MouseWheelZoomHandler(object sender, bool e)
  173. {
  174. double newZoom = CheckZoomLevel(PDFViewer.ZoomFactor + (e ? 0.01 : -0.01), e);
  175. PDFViewer.Zoom(newZoom);
  176. }
  177. private double CheckZoomLevel(double zoom, bool IsGrowth)
  178. {
  179. double standardZoom = 100;
  180. if (zoom <= 0.01)
  181. {
  182. return 0.01;
  183. }
  184. if (zoom >= 10)
  185. {
  186. return 10;
  187. }
  188. zoom *= 100;
  189. for (int i = 0; i < zoomLevel.Length - 1; i++)
  190. {
  191. if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth)
  192. {
  193. standardZoom = zoomLevel[i + 1];
  194. break;
  195. }
  196. if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth)
  197. {
  198. standardZoom = zoomLevel[i];
  199. break;
  200. }
  201. }
  202. return standardZoom / 100;
  203. }
  204. private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  205. {
  206. //不处于页面编辑模式下时,根据PDFVIewer的undo redo状态来更新按钮状态
  207. //页面编辑模式下,按钮状态根据页面编辑的undo redo来显示
  208. if (e.PropertyName == "CanUndo")
  209. {
  210. CanUndo = PDFViewer.UndoManager.CanUndo;
  211. }
  212. if (e.PropertyName == "CanRedo")
  213. {
  214. CanRedo = PDFViewer.UndoManager.CanRedo;
  215. }
  216. if (e.PropertyName == "CanSave")
  217. {
  218. CanSave = PDFViewer.UndoManager.CanSave;
  219. }
  220. }
  221. }
  222. }