HomeContentViewModel.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 DelegateCommand CreateFromHtmlCommnd { get; set; }
  47. public DelegateCommand CreateFromScanner { get; set; }
  48. public IRegionManager toolregion;
  49. public IEventAggregator eventer;
  50. public HomeContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
  51. {
  52. toolregion = regionManager;
  53. eventer = eventAggregator;
  54. ToolRegionName = RegionNames.ToolRegionName;
  55. OpenFileCommand = new DelegateCommand(OpenFile);
  56. ShowToolCommand = new DelegateCommand<string>(ShowToolContent);
  57. CreateBlackPDFCommand = new DelegateCommand(CreatBlankPDF);
  58. CreateFromOtherFile = new DelegateCommand(createFromOtherFile);
  59. CreateFromHtmlCommnd = new DelegateCommand(createFormHtml);
  60. CreateFromScanner = new DelegateCommand(createFromScanner);
  61. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  62. {
  63. toolregion.RequestNavigate(ToolRegionName, "Guid");
  64. }));
  65. }
  66. /// <summary>
  67. /// 显示右侧不同的工具栏页面
  68. /// </summary>
  69. /// <param name="view"></param>
  70. public void ShowToolContent(string view)
  71. {
  72. toolregion.RequestNavigate(ToolRegionName, view);
  73. }
  74. /// <summary>
  75. /// 从其他格式文件创建PDF
  76. /// </summary>
  77. private async void createFromOtherFile()
  78. {
  79. string txt = Properties.Resources.txtex;
  80. string word = Properties.Resources.wordex;
  81. string ppt = Properties.Resources.pptex;
  82. string excel = Properties.Resources.excelex;
  83. string html = Properties.Resources.htmlex;
  84. string image = Properties.Resources.imageex;
  85. string allfiles = txt + word + excel + ppt + image+html;
  86. OpenFileDialog dialog = new OpenFileDialog();
  87. dialog.Multiselect = true;
  88. dialog.Filter = string.Format($"Files({allfiles.Replace(";",",")}|{allfiles})|" +
  89. $"Microsoft Office Word({word})|{word}|" +
  90. $"Microsoft Office Excel({excel})|{excel}|" +
  91. $"Microsoft Office PowerPoint({ppt})|{ppt}|" +
  92. $"Txt({txt})|{txt}|" +
  93. $"Picture({image})|{image}|" +
  94. $"Html({html})|{html}");
  95. if((bool)dialog.ShowDialog())
  96. {
  97. var fileList = dialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  98. if (fileList.Count <= 0)
  99. return;
  100. mainContentViewModel.OpenFile(fileList[0]);
  101. for (int i = 1; i < fileList.Count(); i++)
  102. {
  103. if (!App.OpenedFileList.Contains(fileList[i]))
  104. {
  105. //需要加一定延时 不然连续添加多个文件时会出现regionname重名的情况 因为多处导航是异步导航,可能绑定还没更新
  106. await Task.Delay(20);
  107. App.mainWindowViewModel.AddTabItem(fileList[i]);
  108. }
  109. ToolMethod.SetFileThumbImg(fileList[i]);
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// 从网页创建PDF文件
  115. /// </summary>
  116. private void createFormHtml()
  117. {
  118. }
  119. /// <summary>
  120. /// 从扫描仪创建
  121. /// </summary>
  122. private void createFromScanner()
  123. {
  124. }
  125. /// <summary>
  126. /// 打开文件
  127. /// </summary>
  128. public async void OpenFile()
  129. {
  130. OpenFileDialog openFileDialog = new OpenFileDialog();
  131. openFileDialog.Filter = Properties.Resources.OpenDialogFilter;
  132. openFileDialog.Multiselect = true;
  133. if ((bool)openFileDialog.ShowDialog())
  134. {
  135. IsLoading = Visibility.Visible;
  136. await Task.Delay(3);
  137. if (openFileDialog.FileNames.Count() == 1)
  138. {
  139. if (App.OpenedFileList.Contains(openFileDialog.FileName))
  140. {
  141. App.mainWindowViewModel.SelectItem(openFileDialog.FileName);
  142. }
  143. else
  144. {
  145. mainContentViewModel.OpenFile(openFileDialog.FileName);
  146. }
  147. ToolMethod.SetFileThumbImg(openFileDialog.FileName);
  148. }
  149. else
  150. {
  151. var fileList = openFileDialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  152. if (fileList.Count <= 0)
  153. return;
  154. mainContentViewModel.OpenFile(fileList[0]);
  155. for (int i = 1; i < fileList.Count(); i++)
  156. {
  157. if (!App.OpenedFileList.Contains(fileList[i]))
  158. {
  159. //需要加一定延时 不然连续添加多个文件时会出现regionname重名的情况 因为多处导航是异步导航,可能绑定还没更新
  160. await Task.Delay(20);
  161. App.mainWindowViewModel.AddTabItem(fileList[i]);
  162. }
  163. ToolMethod.SetFileThumbImg(fileList[i]);
  164. }
  165. }
  166. IsLoading = Visibility.Collapsed;
  167. }
  168. }
  169. /// <summary>
  170. /// 创建空白文档
  171. /// </summary>
  172. public void CreatBlankPDF()
  173. {
  174. mainContentViewModel.CreateFile();
  175. }
  176. #region Navigate
  177. public void OnNavigatedTo(NavigationContext navigationContext)
  178. {
  179. var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel;
  180. if (mainVM != null)
  181. {
  182. mainContentViewModel = mainVM;
  183. }
  184. }
  185. public bool IsNavigationTarget(NavigationContext navigationContext)
  186. {
  187. return true;
  188. }
  189. public void OnNavigatedFrom(NavigationContext navigationContext)
  190. {
  191. }
  192. #endregion
  193. }
  194. }