MainContentViewModel.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using ComPDFKitViewer.PdfViewer;
  2. using Microsoft.Win32;
  3. using PDF_Office.CustomControl;
  4. using PDF_Office.EventAggregators;
  5. using PDF_Office.Views;
  6. using Prism.Commands;
  7. using Prism.Events;
  8. using Prism.Ioc;
  9. using Prism.Mvvm;
  10. using Prism.Regions;
  11. using Prism.Services.Dialogs;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Diagnostics;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using PDF_Office.Model;
  21. using System.ComponentModel;
  22. using PDF_Office.Helper;
  23. using PDFSettings.Settings;
  24. namespace PDF_Office.ViewModels
  25. {
  26. public class MainContentViewModel : BindableBase, INavigationAware
  27. {
  28. private string fileName = "Home";
  29. public string FileName
  30. {
  31. get { return fileName; }
  32. set { SetProperty(ref fileName, value); }
  33. }
  34. private string filePath;
  35. public string FilePath
  36. {
  37. get { return filePath; }
  38. set
  39. {
  40. SetProperty(ref filePath, value);
  41. if (!string.IsNullOrEmpty(filePath))
  42. {
  43. FileName = System.IO.Path.GetFileName(filePath);
  44. }
  45. }
  46. }
  47. private Visibility fileChanged = Visibility.Collapsed;
  48. public Visibility FileChanged
  49. {
  50. get { return fileChanged; }
  51. set { SetProperty(ref fileChanged, value); }
  52. }
  53. public CPDFViewer PDFViewer { get; set; }
  54. public DelegateCommand<object> CloseTab { get; set; }
  55. public DelegateCommand<object> Loaded { get; set; }
  56. private string regionName;
  57. public string MainContentRegionName
  58. {
  59. get { return regionName; }
  60. set { SetProperty(ref regionName, value); }
  61. }
  62. public IRegionManager toolregion;
  63. public IEventAggregator eventer;
  64. public IContainerProvider container;
  65. public IDialogService dialogs;
  66. public MainContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IContainerProvider containerProvider, IDialogService dialogService)
  67. {
  68. toolregion = regionManager;
  69. eventer = eventAggregator;
  70. container = containerProvider;
  71. dialogs = dialogService;
  72. CloseTab = new DelegateCommand<object>(CloseTabItem);
  73. MainContentRegionName = Guid.NewGuid().ToString();
  74. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  75. {
  76. NavigationParameters parameters = new NavigationParameters
  77. {
  78. {
  79. "MainViewModel", this
  80. }
  81. };
  82. if (toolregion.Regions.ContainsRegionWithName(MainContentRegionName))
  83. toolregion.RequestNavigate(MainContentRegionName, "HomeContent", parameters);
  84. }));
  85. }
  86. private void CloseTabItem(object item)
  87. {
  88. App.mainWindowViewModel?.CloseTabItem(item);
  89. App.OpenedFileList.Remove(FilePath);
  90. }
  91. public void OpenFile(string filePath)
  92. {
  93. var result = LoadFileFormPath(filePath);
  94. if (!result)
  95. {
  96. return;
  97. }
  98. FilePath = filePath;
  99. NavigationParameters parameters = new NavigationParameters {
  100. { ParameterNames.MainViewModel, this },
  101. { ParameterNames.PDFViewer,PDFViewer}
  102. };
  103. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  104. {
  105. if (toolregion.Regions.ContainsRegionWithName(MainContentRegionName))
  106. toolregion.RequestNavigate(MainContentRegionName, "ViewContent", parameters);
  107. }));
  108. //检查是否是新文档
  109. OpenFileInfo isnew = SettingHelper.GetFileInfo(filePath);
  110. if (isnew == null)
  111. {
  112. isNewDocument = true;
  113. if(App.OpenedFileList.Contains(filePath) == false)
  114. App.OpenedFileList.Add(filePath);
  115. SettingHelper.SortRecentOpenFiles(filePath);
  116. }
  117. }
  118. private bool isNewDocument = false;
  119. /// <summary>
  120. /// 从文件路径创建PDFViewer对象,已包含文档解密逻辑
  121. /// </summary>
  122. /// <param name="path"></param>
  123. /// <returns></returns>
  124. private bool LoadFileFormPath(string path)
  125. {
  126. PDFViewer = new CPDFViewer();
  127. PDFViewer.InitDocument(path);
  128. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  129. if (PDFViewer.Document == null)
  130. {
  131. //MessageBoxEx.Show(App.MainPageLoader.GetString("Main_OpenFileFailedWarning"));
  132. return false;
  133. }
  134. else
  135. {
  136. if (PDFViewer.Document.IsLocked)
  137. {
  138. DialogParameters value = new DialogParameters();
  139. value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  140. dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e =>
  141. {
  142. if (e.Result == ButtonResult.OK)
  143. {
  144. if (e.Parameters.ContainsKey(ParameterNames.PassWord) && e.Parameters.GetValue<string>(ParameterNames.PassWord) != null)
  145. {
  146. PDFViewer.Tag = e.Parameters.GetValue<string>(ParameterNames.PassWord).ToString();
  147. }
  148. }
  149. });
  150. if (PDFViewer.Document.IsLocked)
  151. {
  152. //未成功解密文档时,释放Document对象,返回
  153. PDFViewer.Document.Release();
  154. return false;
  155. }
  156. }
  157. }
  158. PDFViewer.Load();
  159. if (App.mainWindowViewModel != null)
  160. {
  161. App.mainWindowViewModel.CurrentPDFViewer = PDFViewer;
  162. }
  163. App.OpenedFileList.Add(path);
  164. return true;
  165. }
  166. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  167. {
  168. if (e.PropertyName == "CanSave")
  169. {
  170. if (PDFViewer.UndoManager.CanSave)
  171. {
  172. FileChanged = Visibility.Visible;
  173. }
  174. }
  175. }
  176. public void OnNavigatedTo(NavigationContext navigationContext)
  177. {
  178. if (navigationContext.Parameters.Count <= 0)
  179. return;
  180. var filepath = navigationContext.Parameters[ParameterNames.FilePath];
  181. if (filepath != null)
  182. {
  183. OpenFile(filepath.ToString());
  184. }
  185. }
  186. public bool IsNavigationTarget(NavigationContext navigationContext)
  187. {
  188. return false;
  189. }
  190. public void OnNavigatedFrom(NavigationContext navigationContext)
  191. {
  192. }
  193. }
  194. }