BOTAContentViewModel.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. private 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. viewContentViewModel.OpenBOTA = true;
  110. }
  111. }
  112. }
  113. private void InitString()
  114. {
  115. Tip_Annote = App.MainPageLoader.GetString("ViewTopToolbar_Annotation");
  116. Tip_OutLine = App.MainPageLoader.GetString("Outline_Title");
  117. Tip_BookMark = App.MainPageLoader.GetString("Bookmark_Title");
  118. Tip_Search = App.MainPageLoader.GetString("ViewRightMenu_Search");
  119. Tip_Thumbnail = App.MainPageLoader.GetString("Thumbnails_Title");
  120. }
  121. /// <summary>
  122. /// 初始化名称-视图字典
  123. /// </summary>
  124. private void InitDictionartViewNameByTabItem(ref Dictionary<string, string> viewNameByTabItem)
  125. {
  126. //绑定tabitem名字和对应的View控件名称
  127. viewNameByTabItem.Add("TabItemThumbnail", "PageEditContent");
  128. viewNameByTabItem.Add("TabItemOutLine", "OutLineControl");
  129. viewNameByTabItem.Add("TabItemBookMark", "BookmarkContent");
  130. viewNameByTabItem.Add("TabItemAnnotation", "AnnotationContent");
  131. viewNameByTabItem.Add("TabItemSearch", "SearchContent");
  132. viewNameByTabItem.Add("TabItemForm", "");
  133. viewNameByTabItem.Add("TabItemSign", "");
  134. }
  135. /// <summary>
  136. /// 导航至目标TabItem
  137. /// </summary>
  138. /// <param name="currentBar"></param>
  139. private void EnterSelectedBar(string currentBar)
  140. {
  141. CurrentBar = currentBar;
  142. NavigationParameters param = new NavigationParameters();
  143. param.Add(ParameterNames.PDFViewer, pdfViewer);
  144. param.Add(ParameterNames.ViewContentViewModel, viewContentViewModel);
  145. if (currentBar == "TabItemThumbnail")
  146. {
  147. param.Add(ParameterNames.BOTAThumb, true);
  148. }
  149. regions.RequestNavigate(BOTAContentRegionName, viewNameByTabItem[currentBar], param);
  150. var info = SettingHelper.GetFileInfo(pdfViewer.Document.FilePath);
  151. if (info != null)
  152. {
  153. info.BOTASelectedTab = currentBar;
  154. info.BOTASelectedInex = SelectedIndex;
  155. SettingHelper.SetFileInfo(info);
  156. }
  157. }
  158. #region Navigate
  159. public bool IsNavigationTarget(NavigationContext navigationContext)
  160. {
  161. return true;
  162. }
  163. public void OnNavigatedFrom(NavigationContext navigationContext)
  164. {
  165. }
  166. public void OnNavigatedTo(NavigationContext navigationContext)
  167. {
  168. var contentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
  169. if (contentViewModel != null)
  170. {
  171. viewContentViewModel = contentViewModel;
  172. }
  173. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  174. if (pdfview != null)
  175. {
  176. pdfViewer = pdfview;
  177. }
  178. //大纲和文档同时打开
  179. if (Settings.Default.AppProperties.InitialVIew.ShowOutLine && pdfViewer.Document.GetOutlineList().Count > 0)
  180. {
  181. SelectedIndex = 2;
  182. EnterSelectedBar("TabItemOutLine");
  183. viewContentViewModel.OpenBOTA = true;
  184. }
  185. //记录上一次BOTA展开情况
  186. if(Settings.Default.AppProperties.InitialVIew.RememberBOTA)
  187. {
  188. var info = SettingHelper.GetFileInfo(pdfview.Document.FilePath);
  189. //避免新文档因打开文件没有selectedTab信息导致崩溃
  190. if (info != null&!string.IsNullOrEmpty(info.BOTASelectedTab))
  191. {
  192. SelectedIndex = info.BOTASelectedInex;
  193. EnterSelectedBar(info.BOTASelectedTab);
  194. viewContentViewModel.OpenBOTA = true;
  195. }
  196. }
  197. }
  198. #endregion Navigate
  199. }
  200. }