MainWindowViewModel.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using Microsoft.Win32;
  2. using PDF_Office.CustomControl.SystemControl;
  3. using PDF_Office.Model;
  4. using PDF_Office.EventAggregators;
  5. using PDF_Office.Views;
  6. using Prism.Commands;
  7. using Prism.Events;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows.Controls;
  17. using ComPDFKitViewer.PdfViewer;
  18. using Dragablz;
  19. using System.Windows;
  20. using Prism.Ioc;
  21. namespace PDF_Office.ViewModels
  22. {
  23. public class MainWindowViewModel : BindableBase
  24. {
  25. /// <summary>
  26. /// 当前选中项
  27. /// </summary>
  28. //public MainContent SelectedItem { get; set; }
  29. private MainContent selectedItem;
  30. public MainContent SelectedItem
  31. {
  32. get { return selectedItem; }
  33. set {
  34. SetProperty(ref selectedItem, value);
  35. if (SelectedItem!=null)
  36. {
  37. var pdfviewer = (SelectedItem.DataContext as MainContentViewModel).PDFViewer;
  38. if(pdfviewer!=null)
  39. {
  40. CurrentPDFViewer = pdfviewer;
  41. }
  42. }
  43. }
  44. }
  45. private CPDFViewer pdfViewer;
  46. /// <summary>
  47. /// 当前页签的PdfViewer对象
  48. /// </summary>
  49. public CPDFViewer CurrentPDFViewer
  50. {
  51. get { return pdfViewer; }
  52. set { pdfViewer = value; }
  53. }
  54. public DelegateCommand AddTab { get; set; }
  55. public IRegionManager region;
  56. public IEventAggregator eventer;
  57. private IContainerProvider containerProvider;
  58. public IInterTabClient InterTabClient { get; }
  59. public MainWindowViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IContainerProvider container)
  60. {
  61. App.mainWindowViewModel = this;
  62. region = regionManager;
  63. eventer = eventAggregator;
  64. containerProvider = container;
  65. InterTabClient = new InterTabClient(container);
  66. AddTab = new DelegateCommand(AddTabItem);
  67. //第一次打开时需要自动加载Home页
  68. if (App.IsFirstOpen)
  69. {
  70. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  71. {
  72. region.RequestNavigate(RegionNames.MainRegion, "MainContent");
  73. }
  74. ));
  75. App.IsFirstOpen = false;
  76. }
  77. }
  78. /// <summary>
  79. /// 单击+号时添加页签
  80. /// </summary>
  81. private void AddTabItem()
  82. {
  83. region.RequestNavigate(RegionNames.MainRegion, "MainContent");
  84. }
  85. /// <summary>
  86. /// 选择多文档打开时
  87. /// </summary>
  88. /// <param name="filePath"></param>
  89. public void AddTabItem(string filePath)
  90. {
  91. NavigationParameters parameters = new NavigationParameters
  92. {
  93. { ParameterNames.FilePath, filePath }
  94. };
  95. region.RequestNavigate(RegionNames.MainRegion, "MainContent", parameters);
  96. }
  97. public void CloseTabItem(object item)
  98. {
  99. if (region.Regions[RegionNames.MainRegion].Views.Count() > 1)
  100. {
  101. region.Regions[RegionNames.MainRegion].Remove(item);
  102. }
  103. else
  104. {
  105. App.Current.MainWindow.Close();
  106. }
  107. return;
  108. }
  109. public void SelectItem(string filepath)
  110. {
  111. var item = region.Regions[RegionNames.MainRegion].Views.Where(t => ((t as MainContent).DataContext as MainContentViewModel).FilePath == filepath).FirstOrDefault() as MainContent;
  112. if (item == null)
  113. return;
  114. region.Regions[RegionNames.MainRegion].Activate(item);
  115. var behavior = region.Regions[RegionNames.MainRegion].Behaviors[DragablzWindowBehavior.BehaviorKey] as DragablzWindowBehavior;
  116. if(behavior!=null)
  117. {
  118. behavior.ActivateView(item);
  119. }
  120. }
  121. }
  122. }