MainContentViewModel.cs 7.0 KB

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