BOTAContentViewModel.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using Prism.Mvvm;
  2. using Prism.Regions;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using ComPDFKitViewer.PdfViewer;
  9. using PDF_Master.Model;
  10. using Prism.Commands;
  11. using System.Windows.Controls;
  12. using DryIoc;
  13. using Prism.Services.Dialogs;
  14. using PDF_Master.Views.PageEdit;
  15. using PDF_Master.Properties;
  16. using PDF_Master.Helper;
  17. namespace PDF_Master.ViewModels.BOTA
  18. {
  19. public class BOTAContentViewModel : BindableBase, INavigationAware
  20. {
  21. public CPDFViewer pdfViewer { get; set; }
  22. private IRegionManager regions { get; set; }
  23. private IDialogService dialogs { get; set; }
  24. public ViewContentViewModel viewContentViewModel { get; set; }
  25. private Dictionary<string, string> viewNameByTabItem;
  26. private string botaContentRegionName;
  27. public string BOTAContentRegionName
  28. {
  29. get { return botaContentRegionName; }
  30. set
  31. {
  32. SetProperty(ref botaContentRegionName, value);
  33. }
  34. }
  35. private int selectedIndex;
  36. public int SelectedIndex
  37. {
  38. get { return selectedIndex; }
  39. set
  40. {
  41. SetProperty(ref selectedIndex, value);
  42. }
  43. }
  44. private string tip_Thumbnail;
  45. public string Tip_Thumbnail
  46. {
  47. get { return tip_Thumbnail; }
  48. set
  49. {
  50. SetProperty(ref tip_Thumbnail, value);
  51. }
  52. }
  53. private string tip_OutLine;
  54. public string Tip_OutLine
  55. {
  56. get { return tip_OutLine; }
  57. set
  58. {
  59. SetProperty(ref tip_OutLine, value);
  60. }
  61. }
  62. private string tip_BookMark;
  63. public string Tip_BookMark
  64. {
  65. get { return tip_BookMark; }
  66. set
  67. {
  68. SetProperty(ref tip_BookMark, value);
  69. }
  70. }
  71. private string tip_Search;
  72. public string Tip_Search
  73. {
  74. get { return tip_Search; }
  75. set
  76. {
  77. SetProperty(ref tip_Search, value);
  78. }
  79. }
  80. private string tip_Annote;
  81. public string Tip_Annote
  82. {
  83. get { return tip_Annote; }
  84. set
  85. {
  86. SetProperty(ref tip_Annote, value);
  87. }
  88. }
  89. public string CurrentBar = "";
  90. public DelegateCommand<object> TabControlSelectionChangedCommand { get; set; }
  91. public BOTAContentViewModel(IRegionManager regionManager, IDialogService dialogService)
  92. {
  93. regions = regionManager;
  94. dialogs = dialogService;
  95. BOTAContentRegionName = Guid.NewGuid().ToString();
  96. TabControlSelectionChangedCommand = new DelegateCommand<object>(TabControlSelectionChangedEvent);
  97. viewNameByTabItem = new Dictionary<string, string>();
  98. InitDictionartViewNameByTabItem(ref viewNameByTabItem);
  99. InitString();
  100. }
  101. private void TabControlSelectionChangedEvent(object e)
  102. {
  103. var args = e as SelectionChangedEventArgs;
  104. if (args != null&&args.AddedItems.Count>0)
  105. {
  106. if (!string.IsNullOrEmpty((args.AddedItems[0] as TabItem).Name))
  107. {
  108. EnterSelectedBar((args.AddedItems[0] as TabItem).Name);
  109. if((args.AddedItems[0] as TabItem).IsSelected)
  110. {
  111. viewContentViewModel.OpenBOTA = true;
  112. }
  113. }
  114. }
  115. }
  116. private void InitString()
  117. {
  118. Tip_Annote = App.MainPageLoader.GetString("ViewTopToolbar_Annotation");
  119. Tip_OutLine = App.MainPageLoader.GetString("Outline_Title");
  120. Tip_BookMark = App.MainPageLoader.GetString("Bookmark_Title");
  121. Tip_Search = App.MainPageLoader.GetString("ViewRightMenu_Search");
  122. Tip_Thumbnail = App.MainPageLoader.GetString("Thumbnails_Title");
  123. }
  124. /// <summary>
  125. /// 初始化名称-视图字典
  126. /// </summary>
  127. private void InitDictionartViewNameByTabItem(ref Dictionary<string, string> viewNameByTabItem)
  128. {
  129. //绑定tabitem名字和对应的View控件名称
  130. viewNameByTabItem.Add("TabItemThumbnail", "PageEditContent");
  131. viewNameByTabItem.Add("TabItemOutLine", "OutLineControl");
  132. viewNameByTabItem.Add("TabItemBookMark", "BookmarkContent");
  133. viewNameByTabItem.Add("TabItemAnnotation", "AnnotationContent");
  134. viewNameByTabItem.Add("TabItemSearch", "SearchContent");
  135. viewNameByTabItem.Add("TabItemForm", "");
  136. viewNameByTabItem.Add("TabItemSign", "");
  137. }
  138. /// <summary>
  139. /// 导航至目标TabItem
  140. /// </summary>
  141. /// <param name="currentBar"></param>
  142. private void EnterSelectedBar(string currentBar)
  143. {
  144. //增加判空逻辑
  145. if (!string.IsNullOrEmpty(currentBar))
  146. {
  147. CurrentBar = currentBar;
  148. NavigationParameters param = new NavigationParameters();
  149. param.Add(ParameterNames.PDFViewer, pdfViewer);
  150. param.Add(ParameterNames.ViewContentViewModel, viewContentViewModel);
  151. if (currentBar == "TabItemThumbnail")
  152. {
  153. param.Add(ParameterNames.BOTAThumb, true);
  154. }
  155. if (viewNameByTabItem.ContainsKey(currentBar))
  156. {
  157. regions.RequestNavigate(BOTAContentRegionName, viewNameByTabItem[currentBar], param);
  158. }
  159. //增加判空逻辑
  160. if (pdfViewer != null && pdfViewer.Document != null)
  161. {
  162. var info = SettingHelper.GetFileInfo(pdfViewer.Document.FilePath);
  163. if (info != null)
  164. {
  165. info.BOTASelectedTab = currentBar;
  166. info.BOTASelectedInex = SelectedIndex;
  167. SettingHelper.SetFileInfo(info);
  168. }
  169. }
  170. }
  171. }
  172. /// <summary>
  173. /// 根据缓存判断,如果记录需要展开BOTA 则还原上一次的展开状态
  174. /// </summary>
  175. public void OpenBOTA()
  176. {
  177. //记录上一次BOTA展开情况
  178. if (Settings.Default.AppProperties.InitialVIew.RememberBOTA)
  179. {
  180. //如果是加密文档 则先不展开,解密后再展开
  181. if(pdfViewer.Document.IsLocked)
  182. {
  183. return;
  184. }
  185. var info = SettingHelper.GetFileInfo(pdfViewer.Document.FilePath);
  186. //避免新文档因打开文件没有selectedTab信息导致崩溃
  187. if (info != null && !string.IsNullOrEmpty(info.BOTASelectedTab))
  188. {
  189. SelectedIndex = info.BOTASelectedInex;
  190. EnterSelectedBar(info.BOTASelectedTab);
  191. viewContentViewModel.OpenBOTA = true;
  192. }
  193. }
  194. }
  195. #region Navigate
  196. public bool IsNavigationTarget(NavigationContext navigationContext)
  197. {
  198. return true;
  199. }
  200. public void OnNavigatedFrom(NavigationContext navigationContext)
  201. {
  202. }
  203. public void OnNavigatedTo(NavigationContext navigationContext)
  204. {
  205. var contentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
  206. if (contentViewModel != null)
  207. {
  208. viewContentViewModel = contentViewModel;
  209. contentViewModel.botaViewModel = this;
  210. }
  211. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  212. if (pdfview != null)
  213. {
  214. pdfViewer = pdfview;
  215. }
  216. //大纲和文档同时打开
  217. if (Settings.Default.AppProperties.InitialVIew.ShowOutLine && pdfViewer.Document.GetOutlineList().Count > 0)
  218. {
  219. SelectedIndex = 2;
  220. EnterSelectedBar("TabItemOutLine");
  221. viewContentViewModel.OpenBOTA = true;
  222. }
  223. //确认是否要展开BOTA
  224. OpenBOTA();
  225. }
  226. #endregion Navigate
  227. }
  228. }