MainContentViewModel.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. private Visibility isLoading = Visibility.Collapsed;
  54. public Visibility IsLoading
  55. {
  56. get { return isLoading; }
  57. set
  58. {
  59. SetProperty(ref isLoading, value);
  60. }
  61. }
  62. public CPDFViewer PDFViewer { get; set; }
  63. public DelegateCommand<object> CloseTab { get; set; }
  64. public DelegateCommand<object> Loaded { get; set; }
  65. private string regionName;
  66. public string MainContentRegionName
  67. {
  68. get { return regionName; }
  69. set { SetProperty(ref regionName, value); }
  70. }
  71. public IRegionManager toolregion;
  72. public IEventAggregator eventer;
  73. public IContainerProvider container;
  74. public IDialogService dialogs;
  75. public MainContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IContainerProvider containerProvider,IDialogService dialogService)
  76. {
  77. toolregion = regionManager;
  78. eventer = eventAggregator;
  79. container = containerProvider;
  80. dialogs = dialogService;
  81. CloseTab = new DelegateCommand<object>(CloseTabItem);
  82. MainContentRegionName = Guid.NewGuid().ToString();
  83. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  84. {
  85. NavigationParameters parameters = new NavigationParameters
  86. {
  87. {
  88. "MainViewModel", this
  89. }
  90. };
  91. if (toolregion.Regions.ContainsRegionWithName(MainContentRegionName))
  92. toolregion.RequestNavigate(MainContentRegionName, "HomeContent", parameters);
  93. }));
  94. }
  95. private void CloseTabItem(object item)
  96. {
  97. App.mainWindowViewModel?.CloseTabItem(item);
  98. App.OpenedFileList.Remove(FilePath);
  99. }
  100. public void OpenFile(string filePath)
  101. {
  102. var result = LoadFileFormPath(filePath);
  103. if (!result)
  104. {
  105. IsLoading = Visibility.Collapsed;
  106. return;
  107. }
  108. FilePath = filePath;
  109. NavigationParameters parameters = new NavigationParameters {
  110. { ParameterNames.MainViewModel, this },
  111. { ParameterNames.PDFViewer,PDFViewer}
  112. };
  113. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  114. {
  115. if (toolregion.Regions.ContainsRegionWithName(MainContentRegionName))
  116. toolregion.RequestNavigate(MainContentRegionName, "ViewContent", parameters);
  117. }));
  118. //检查是否是新文档
  119. OpenFileInfo isnew = SettingHelper.GetFileInfo(filePath);
  120. if (isnew == null)
  121. {
  122. isNewDocument = true;
  123. App.OpenedFileList.Add(filePath);
  124. SettingHelper.SortRecentOpenFiles(filePath);
  125. }
  126. }
  127. private bool isNewDocument = false;
  128. /// <summary>
  129. /// 从文件路径创建PDFViewer对象,已包含文档解密逻辑
  130. /// </summary>
  131. /// <param name="path"></param>
  132. /// <returns></returns>
  133. private bool LoadFileFormPath(string path)
  134. {
  135. PDFViewer = new CPDFViewer();
  136. PDFViewer.InitDocument(path);
  137. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  138. if (PDFViewer.Document == null)
  139. {
  140. //MessageBoxEx.Show(App.MainPageLoader.GetString("Main_OpenFileFailedWarning"));
  141. return false;
  142. }
  143. else
  144. {
  145. if (PDFViewer.Document.IsLocked)
  146. {
  147. DialogParameters value = new DialogParameters();
  148. value.Add(ParameterNames.PDFDocument,PDFViewer.Document);
  149. dialogs.ShowDialog(DialogNames.VerifyPassWordDialog,value,e=> {
  150. if(e.Result== ButtonResult.OK)
  151. {
  152. if(e.Parameters.ContainsKey(ParameterNames.PassWord)&&e.Parameters.GetValue<string>(ParameterNames.PassWord) !=null)
  153. {
  154. PDFViewer.Tag = e.Parameters.GetValue<string>(ParameterNames.PassWord).ToString();
  155. }
  156. }
  157. });
  158. if (PDFViewer.Document.IsLocked)
  159. {
  160. //未成功解密文档时,释放Document对象,返回
  161. PDFViewer.Document.Release();
  162. return false;
  163. }
  164. }
  165. }
  166. PDFViewer.Load();
  167. if (App.mainWindowViewModel != null)
  168. {
  169. App.mainWindowViewModel.CurrentPDFViewer = PDFViewer;
  170. }
  171. App.OpenedFileList.Add(path);
  172. return true;
  173. }
  174. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  175. {
  176. if (e.PropertyName == "CanSave")
  177. {
  178. if( PDFViewer.UndoManager.CanSave)
  179. {
  180. FileChanged = Visibility.Visible;
  181. }
  182. }
  183. }
  184. public void OnNavigatedTo(NavigationContext navigationContext)
  185. {
  186. if (navigationContext.Parameters.Count <= 0)
  187. return;
  188. var filepath = navigationContext.Parameters[ParameterNames.FilePath];
  189. if (filepath != null)
  190. {
  191. OpenFile(filepath.ToString());
  192. }
  193. }
  194. public bool IsNavigationTarget(NavigationContext navigationContext)
  195. {
  196. return false;
  197. }
  198. public void OnNavigatedFrom(NavigationContext navigationContext)
  199. {
  200. }
  201. }
  202. }