HomeContentViewModel.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Microsoft.Win32;
  2. using PDF_Office.EventAggregators;
  3. using Prism.Commands;
  4. using Prism.Events;
  5. using Prism.Mvvm;
  6. using Prism.Regions;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace PDF_Office.ViewModels
  13. {
  14. public class HomeContentViewModel : BindableBase, INavigationAware
  15. {
  16. private string fileName = "Home";
  17. public string FileName
  18. {
  19. get { return fileName; }
  20. set { SetProperty(ref fileName, value); }
  21. }
  22. private string regionName;
  23. public string RegionName
  24. {
  25. get { return regionName; }
  26. set { SetProperty(ref regionName, value); }
  27. }
  28. private MainContentViewModel mainContentViewModel;
  29. public DelegateCommand OpenFileCommand { get; set; }
  30. public DelegateCommand<string> ShowToolCommand { get; set; }
  31. public IRegionManager toolregion;
  32. public IEventAggregator eventer;
  33. public HomeContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
  34. {
  35. toolregion = regionManager;
  36. eventer = eventAggregator;
  37. RegionName = Guid.NewGuid().ToString();
  38. OpenFileCommand = new DelegateCommand(OpenFile);
  39. ShowToolCommand = new DelegateCommand<string>(ShowToolContent);
  40. }
  41. /// <summary>
  42. /// 显示右侧不同的工具栏页面
  43. /// </summary>
  44. /// <param name="view"></param>
  45. public void ShowToolContent(string view)
  46. {
  47. toolregion.RequestNavigate(RegionName, view);
  48. }
  49. /// <summary>
  50. /// 打开文件
  51. /// </summary>
  52. public void OpenFile()
  53. {
  54. OpenFileDialog openFileDialog = new OpenFileDialog();
  55. openFileDialog.Filter = "PDF Files (*.pdf)|*.pdf";
  56. openFileDialog.Multiselect = true;
  57. if ((bool)openFileDialog.ShowDialog())
  58. {
  59. if (openFileDialog.FileNames.Count() == 1)
  60. {
  61. if (App.OpenedFileList.Contains(openFileDialog.FileName))
  62. {
  63. App.mainWindowViewModel.SelectItem(openFileDialog.FileName);
  64. }
  65. else
  66. {
  67. mainContentViewModel.OpenFile(openFileDialog.FileName);
  68. }
  69. }
  70. else
  71. {
  72. var fileList = openFileDialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  73. if (fileList.Count <= 0)
  74. return;
  75. mainContentViewModel.OpenFile(fileList[0]);
  76. for (int i = 1; i < fileList.Count(); i++)
  77. {
  78. if (!App.OpenedFileList.Contains(fileList[i]))
  79. {
  80. App.mainWindowViewModel.AddTabItem(fileList[i]);
  81. }
  82. }
  83. }
  84. }
  85. }
  86. public void CreatBlankPDF()
  87. {
  88. }
  89. public void OnNavigatedTo(NavigationContext navigationContext)
  90. {
  91. var mainVM = navigationContext.Parameters["MainViewModel"] as MainContentViewModel;
  92. if (mainVM != null)
  93. {
  94. mainContentViewModel = mainVM;
  95. }
  96. }
  97. public bool IsNavigationTarget(NavigationContext navigationContext)
  98. {
  99. return true;
  100. }
  101. public void OnNavigatedFrom(NavigationContext navigationContext)
  102. {
  103. }
  104. }
  105. }