ViewContentViewModel.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 System.Windows;
  17. using System.Windows.Controls;
  18. namespace PDF_Office.ViewModels
  19. {
  20. public class ViewContentViewModel : BindableBase, INavigationAware
  21. {
  22. #region 属性、变量
  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. public string ToolContentRegionName { get; set; }
  31. private int gridToolRow = 1;
  32. /// <summary>
  33. /// 控制ToolContent的Row
  34. /// </summary>
  35. public int GridToolRow
  36. {
  37. get { return gridToolRow; }
  38. set
  39. {
  40. SetProperty(ref gridToolRow, value);
  41. }
  42. }
  43. private int gridToolRowSpan = 3;
  44. /// <summary>
  45. /// 控制ToolContent的RowSpan
  46. /// </summary>
  47. public int GridToolRowSpan
  48. {
  49. get { return gridToolRowSpan; }
  50. set
  51. {
  52. SetProperty(ref gridToolRowSpan, value);
  53. }
  54. }
  55. private Visibility toolContentVisible = Visibility.Visible;
  56. /// <summary>
  57. /// 控制Content的显示 用于显示水印、贝茨码、密文等功能模块
  58. /// 留意:显示前需要先注入内容、设置好行和跨行数
  59. /// </summary>
  60. public Visibility ToolContentVisible
  61. {
  62. get { return toolContentVisible; }
  63. set
  64. {
  65. SetProperty(ref toolContentVisible, value);
  66. }
  67. }
  68. private Visibility isLoading = Visibility.Collapsed;
  69. /// <summary>
  70. /// 是否正在加载中
  71. /// </summary>
  72. public Visibility IsLoading
  73. {
  74. get { return isLoading; }
  75. set
  76. {
  77. SetProperty(ref isLoading, value);
  78. }
  79. }
  80. /// <summary>
  81. /// 用来避免重复触发导航事件的标志符
  82. /// </summary>
  83. private bool isOpenFile = false;
  84. /// <summary>
  85. /// 鼠标滚轮缩放的缩放值
  86. /// </summary>
  87. private double[] zoomLevel = { 1.00f, 10, 25, 50, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  88. #endregion
  89. #region 命令
  90. public DelegateCommand LoadFile { get; set; }
  91. public DelegateCommand Load { get; set; }
  92. public DelegateCommand<object> TabControlSelectionChangedCommand { get; set; }
  93. #endregion
  94. public ViewContentViewModel(IRegionManager regionManager, IDialogService dialogService)
  95. {
  96. region = regionManager;
  97. dialogs = dialogService;
  98. LoadFile = new DelegateCommand(loadFile);
  99. Load = new DelegateCommand(LoadControl);
  100. TabControlSelectionChangedCommand = new DelegateCommand<object>(TabControlSelectonChangedEvent);
  101. ViwerRegionName = Guid.NewGuid().ToString();
  102. BOTARegionName = Guid.NewGuid().ToString();
  103. PropertyRegionName = Guid.NewGuid().ToString();
  104. //未显示时无法注册上Region名称
  105. ToolContentVisible = Visibility.Visible;
  106. ToolContentRegionName = Guid.NewGuid().ToString();
  107. ToolContentVisible = Visibility.Collapsed;
  108. }
  109. private void LoadControl()
  110. {
  111. //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域
  112. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  113. {
  114. region.RequestNavigate(BOTARegionName, "BOTAContent");
  115. }
  116. ));
  117. }
  118. /// <summary>
  119. /// 将PDFViwer添加到Region
  120. /// </summary>
  121. private void loadFile()
  122. {
  123. PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
  124. region.AddToRegion(ViwerRegionName, PDFViewer);
  125. }
  126. /// <summary>
  127. /// 选项卡切换事件
  128. /// </summary>
  129. /// <param name="e"></param>
  130. private void TabControlSelectonChangedEvent(object e)
  131. {
  132. var args = e as SelectionChangedEventArgs;
  133. if (args != null)
  134. {
  135. var item = args.AddedItems[0] as TabItem;
  136. if (item.Name == "TabItemPageEdit")
  137. {
  138. EnterToolMode("PageEditContent");
  139. }
  140. else
  141. {
  142. ExitToolMode();
  143. }
  144. }
  145. }
  146. #region PDFViewer鼠标滚轮缩放事件
  147. public void PdfViewer_MouseWheelZoomHandler(object sender, bool e)
  148. {
  149. double newZoom = CheckZoomLevel(PDFViewer.ZoomFactor + (e ? 0.01 : -0.01), e);
  150. PDFViewer.Zoom(newZoom);
  151. }
  152. private double CheckZoomLevel(double zoom, bool IsGrowth)
  153. {
  154. double standardZoom = 100;
  155. if (zoom <= 0.01)
  156. {
  157. return 0.01;
  158. }
  159. if (zoom >= 10)
  160. {
  161. return 10;
  162. }
  163. zoom *= 100;
  164. for (int i = 0; i < zoomLevel.Length - 1; i++)
  165. {
  166. if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth)
  167. {
  168. standardZoom = zoomLevel[i + 1];
  169. break;
  170. }
  171. if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth)
  172. {
  173. standardZoom = zoomLevel[i];
  174. break;
  175. }
  176. }
  177. return standardZoom / 100;
  178. }
  179. #endregion
  180. #region Navigate
  181. public void OnNavigatedTo(NavigationContext navigationContext)
  182. {
  183. if (isOpenFile)
  184. return;
  185. var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel;
  186. if (mainVM != null)
  187. {
  188. mainViewModel = mainVM;
  189. }
  190. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  191. if (pdfview != null)
  192. {
  193. PDFViewer = pdfview;
  194. loadFile();
  195. }
  196. isOpenFile = true;
  197. }
  198. public bool IsNavigationTarget(NavigationContext navigationContext)
  199. {
  200. return true;
  201. }
  202. public void OnNavigatedFrom(NavigationContext navigationContext)
  203. {
  204. }
  205. #endregion
  206. #region 方法
  207. /// <summary>
  208. /// 显示前添加内容到Region
  209. /// </summary>
  210. /// <param name="isPageEdit"></param>
  211. private void ShowToolContent(bool isPageEdit = true)
  212. {
  213. //显示页面编辑
  214. if (isPageEdit)
  215. {
  216. if (GridToolRow != 1)
  217. {
  218. GridToolRow = 1;
  219. }
  220. if (GridToolRowSpan != 3)
  221. {
  222. GridToolRowSpan = 3;
  223. }
  224. }
  225. else
  226. {
  227. //显示其他工具模式 如密文、水印等
  228. GridToolRow = 0;
  229. GridToolRowSpan = 4;
  230. }
  231. ToolContentVisible = Visibility.Visible;
  232. }
  233. /// <summary>
  234. /// 进入工具编辑(如页面编辑、水印、密文等)模式
  235. /// </summary>
  236. /// <param name="targetToolMode">要导航过去的控件名称</param>
  237. /// <param name="valuePairs">导航需要传送的参数,为空时,默认传送PDFView和ViewContentViewModel</param>
  238. private async void EnterToolMode(string targetToolMode, NavigationParameters valuePairs = null)
  239. {
  240. IsLoading = Visibility.Visible;
  241. await Task.Delay(3);
  242. NavigationParameters param = new NavigationParameters();
  243. if (valuePairs == null)
  244. {
  245. param.Add(ParameterNames.PDFViewer, PDFViewer);
  246. param.Add(ParameterNames.ViewContentViewModel, this);
  247. }
  248. else//有传入其他内容的参数时
  249. {
  250. param = valuePairs;
  251. }
  252. region.RequestNavigate(ToolContentRegionName, targetToolMode, param);
  253. if (targetToolMode == "PageEditContent")
  254. {
  255. ShowToolContent();
  256. }
  257. else
  258. {
  259. ShowToolContent(false);
  260. }
  261. IsLoading = Visibility.Collapsed;
  262. }
  263. /// <summary>
  264. /// 退出工具(水印、密文等)编辑模式,隐藏ToolContent
  265. /// </summary>
  266. public void ExitToolMode()
  267. {
  268. ToolContentVisible = Visibility.Collapsed;
  269. }
  270. #endregion
  271. }
  272. }