MainContentViewModel.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using Microsoft.Win32;
  2. using PDF_Office.EventAggregators;
  3. using PDF_Office.Views;
  4. using Prism.Commands;
  5. using Prism.Events;
  6. using Prism.Ioc;
  7. using Prism.Mvvm;
  8. using Prism.Regions;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. namespace PDF_Office.ViewModels
  16. {
  17. public enum LoadType
  18. {
  19. /// <summary>
  20. /// 打开文档
  21. /// </summary>
  22. Open,
  23. /// <summary>
  24. /// 创建文档
  25. /// </summary>
  26. Create
  27. }
  28. public class MainContentViewModel : BindableBase, INavigationAware
  29. {
  30. private string fileName = "Home";
  31. public string FileName
  32. {
  33. get { return fileName; }
  34. set { SetProperty(ref fileName, value); }
  35. }
  36. private string filePath;
  37. public string FilePath
  38. {
  39. get { return filePath; }
  40. set
  41. {
  42. SetProperty(ref filePath, value);
  43. if (!string.IsNullOrEmpty(filePath))
  44. {
  45. FileName = System.IO.Path.GetFileName(filePath);
  46. }
  47. }
  48. }
  49. private Visibility fileChanged;
  50. public Visibility FileChanged
  51. {
  52. get { return fileChanged; }
  53. set { SetProperty(ref fileChanged, value); }
  54. }
  55. public DelegateCommand<object> CloseTab { get; set; }
  56. private string regionName;
  57. public string RegionName
  58. {
  59. get { return regionName; }
  60. set { SetProperty(ref regionName, value); }
  61. }
  62. public IRegionManager toolregion;
  63. public IEventAggregator eventer;
  64. public IContainerProvider container;
  65. public MainContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IContainerProvider containerProvider)
  66. {
  67. toolregion = regionManager;
  68. eventer = eventAggregator;
  69. container = containerProvider;
  70. CloseTab = new DelegateCommand<object>(CloseTabItem);
  71. RegionName = Guid.NewGuid().ToString();
  72. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  73. {
  74. NavigationParameters parameters = new NavigationParameters
  75. {
  76. {
  77. "MainViewModel", this
  78. }
  79. };
  80. toolregion.RequestNavigate(RegionName, "HomeContent", parameters);
  81. }));
  82. }
  83. private void CloseTabItem(object item)
  84. {
  85. App.mainWindowViewModel?.CloseTabItem(item);
  86. App.OpenedFileList.Remove(FilePath);
  87. }
  88. public void OpenFile(string filePath)
  89. {
  90. FilePath = filePath;
  91. NavigationParameters parameters = new NavigationParameters { { "MainViewModel", this } };
  92. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  93. {
  94. toolregion.RequestNavigate(RegionName, "ViewContent", parameters);
  95. }));
  96. }
  97. public void OnNavigatedTo(NavigationContext navigationContext)
  98. {
  99. if (navigationContext.Parameters.Count <= 0)
  100. return;
  101. var filepath = navigationContext.Parameters["filePath"];
  102. if(filepath!=null)
  103. {
  104. OpenFile(filepath.ToString());
  105. }
  106. }
  107. public bool IsNavigationTarget(NavigationContext navigationContext)
  108. {
  109. if (navigationContext.Parameters.Count <= 0)
  110. return true;
  111. var isAddTab = navigationContext.Parameters["AddTab"];
  112. if (isAddTab!=null&&(bool)isAddTab)
  113. {
  114. return false;
  115. }
  116. return true;
  117. }
  118. public void OnNavigatedFrom(NavigationContext navigationContext)
  119. {
  120. }
  121. }
  122. }