HomeContentViewModel.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Microsoft.Win32;
  2. using PDF_Office.EventAggregators;
  3. using PDF_Office.Model;
  4. using Prism.Commands;
  5. using Prism.Events;
  6. using Prism.Mvvm;
  7. using Prism.Regions;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  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 class HomeContentViewModel : BindableBase, INavigationAware
  18. {
  19. private string fileName = "Home";
  20. public string FileName
  21. {
  22. get { return fileName; }
  23. set { SetProperty(ref fileName, value); }
  24. }
  25. private string regionName;
  26. public string ToolRegionName
  27. {
  28. get { return regionName; }
  29. set { SetProperty(ref regionName, value); }
  30. }
  31. private Visibility isLoading = Visibility.Collapsed;
  32. public Visibility IsLoading
  33. {
  34. get { return isLoading; }
  35. set
  36. {
  37. SetProperty(ref isLoading, value);
  38. }
  39. }
  40. private MainContentViewModel mainContentViewModel;
  41. public DelegateCommand OpenFileCommand { get; set; }
  42. public DelegateCommand<string> ShowToolCommand { get; set; }
  43. public IRegionManager toolregion;
  44. public IEventAggregator eventer;
  45. public HomeContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
  46. {
  47. toolregion = regionManager;
  48. eventer = eventAggregator;
  49. ToolRegionName = Guid.NewGuid().ToString();
  50. OpenFileCommand = new DelegateCommand(OpenFile);
  51. ShowToolCommand = new DelegateCommand<string>(ShowToolContent);
  52. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  53. {
  54. toolregion.RequestNavigate(ToolRegionName, "Guid");
  55. }));
  56. }
  57. /// <summary>
  58. /// 显示右侧不同的工具栏页面
  59. /// </summary>
  60. /// <param name="view"></param>
  61. public void ShowToolContent(string view)
  62. {
  63. toolregion.RequestNavigate(ToolRegionName, view);
  64. }
  65. /// <summary>
  66. /// 打开文件
  67. /// </summary>
  68. public async void OpenFile()
  69. {
  70. OpenFileDialog openFileDialog = new OpenFileDialog();
  71. openFileDialog.Filter = Properties.Resources.OpenDialogFilter;
  72. openFileDialog.Multiselect = true;
  73. if ((bool)openFileDialog.ShowDialog())
  74. {
  75. IsLoading = Visibility.Visible;
  76. await Task.Delay(3);
  77. if (openFileDialog.FileNames.Count() == 1)
  78. {
  79. if (App.OpenedFileList.Contains(openFileDialog.FileName))
  80. {
  81. App.mainWindowViewModel.SelectItem(openFileDialog.FileName);
  82. }
  83. else
  84. {
  85. mainContentViewModel.OpenFile(openFileDialog.FileName);
  86. }
  87. }
  88. else
  89. {
  90. var fileList = openFileDialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  91. if (fileList.Count <= 0)
  92. return;
  93. mainContentViewModel.OpenFile(fileList[0]);
  94. for (int i = 1; i < fileList.Count(); i++)
  95. {
  96. if (!App.OpenedFileList.Contains(fileList[i]))
  97. {
  98. App.mainWindowViewModel.AddTabItem(fileList[i]);
  99. }
  100. }
  101. }
  102. IsLoading = Visibility.Collapsed;
  103. }
  104. }
  105. public void CreatBlankPDF()
  106. {
  107. }
  108. #region Navigate
  109. public void OnNavigatedTo(NavigationContext navigationContext)
  110. {
  111. var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel;
  112. if (mainVM != null)
  113. {
  114. mainContentViewModel = mainVM;
  115. }
  116. }
  117. public bool IsNavigationTarget(NavigationContext navigationContext)
  118. {
  119. return true;
  120. }
  121. public void OnNavigatedFrom(NavigationContext navigationContext)
  122. {
  123. }
  124. #endregion
  125. }
  126. }