HomeContentViewModel.cs 5.2 KB

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