ViewContentViewModel.cs 12 KB

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