BOTAContentViewModel.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. CurrentBar = currentBar;
  145. NavigationParameters param = new NavigationParameters();
  146. param.Add(ParameterNames.PDFViewer, pdfViewer);
  147. param.Add(ParameterNames.ViewContentViewModel, viewContentViewModel);
  148. if (currentBar == "TabItemThumbnail")
  149. {
  150. param.Add(ParameterNames.BOTAThumb, true);
  151. }
  152. regions.RequestNavigate(BOTAContentRegionName, viewNameByTabItem[currentBar], param);
  153. var info = SettingHelper.GetFileInfo(pdfViewer.Document.FilePath);
  154. if (info != null)
  155. {
  156. info.BOTASelectedTab = currentBar;
  157. info.BOTASelectedInex = SelectedIndex;
  158. SettingHelper.SetFileInfo(info);
  159. }
  160. }
  161. #region Navigate
  162. public bool IsNavigationTarget(NavigationContext navigationContext)
  163. {
  164. return true;
  165. }
  166. public void OnNavigatedFrom(NavigationContext navigationContext)
  167. {
  168. }
  169. public void OnNavigatedTo(NavigationContext navigationContext)
  170. {
  171. var contentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
  172. if (contentViewModel != null)
  173. {
  174. viewContentViewModel = contentViewModel;
  175. }
  176. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  177. if (pdfview != null)
  178. {
  179. pdfViewer = pdfview;
  180. }
  181. //大纲和文档同时打开
  182. if (Settings.Default.AppProperties.InitialVIew.ShowOutLine && pdfViewer.Document.GetOutlineList().Count > 0)
  183. {
  184. SelectedIndex = 2;
  185. EnterSelectedBar("TabItemOutLine");
  186. viewContentViewModel.OpenBOTA = true;
  187. }
  188. //记录上一次BOTA展开情况
  189. if(Settings.Default.AppProperties.InitialVIew.RememberBOTA)
  190. {
  191. var info = SettingHelper.GetFileInfo(pdfview.Document.FilePath);
  192. //避免新文档因打开文件没有selectedTab信息导致崩溃
  193. if (info != null&&!string.IsNullOrEmpty(info.BOTASelectedTab))
  194. {
  195. SelectedIndex = info.BOTASelectedInex;
  196. EnterSelectedBar(info.BOTASelectedTab);
  197. viewContentViewModel.OpenBOTA = true;
  198. }
  199. }
  200. }
  201. #endregion Navigate
  202. }
  203. }