MainWindowViewModel.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. private MainContent selectedItem;
  26. public MainContent SelectedItem
  27. {
  28. get { return selectedItem; }
  29. set {
  30. SetProperty(ref selectedItem, value);
  31. if (SelectedItem!=null)
  32. {
  33. var pdfviewer = (SelectedItem.DataContext as MainContentViewModel).PDFViewer;
  34. if(pdfviewer!=null)
  35. {
  36. CurrentPDFViewer = pdfviewer;
  37. }
  38. }
  39. }
  40. }
  41. private CPDFViewer pdfViewer;
  42. /// <summary>
  43. /// 当前页签的PdfViewer对象
  44. /// </summary>
  45. public CPDFViewer CurrentPDFViewer
  46. {
  47. get { return pdfViewer; }
  48. set { pdfViewer = value; }
  49. }
  50. public DelegateCommand AddTab { get; set; }
  51. public IRegionManager region;
  52. public IEventAggregator eventer;
  53. private IContainerProvider containerProvider;
  54. public IInterTabClient InterTabClient { get; }
  55. public MainWindowViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IContainerProvider container)
  56. {
  57. if (App.mainWindowViewModel == null)
  58. {
  59. //加载第一个窗体时 先赋值静态对象
  60. App.mainWindowViewModel = this;
  61. }
  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. }