HomeContentViewModel.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 Prism.Services.Dialogs;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Diagnostics;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. namespace PDF_Office.ViewModels
  18. {
  19. public class HomeContentViewModel : BindableBase, INavigationAware
  20. {
  21. private string fileName = "Home";
  22. public string FileName
  23. {
  24. get { return fileName; }
  25. set { SetProperty(ref fileName, value); }
  26. }
  27. private string regionName;
  28. public string ToolRegionName
  29. {
  30. get { return regionName; }
  31. set { SetProperty(ref regionName, value); }
  32. }
  33. private Visibility isLoading = Visibility.Collapsed;
  34. public Visibility IsLoading
  35. {
  36. get { return isLoading; }
  37. set
  38. {
  39. SetProperty(ref isLoading, value);
  40. }
  41. }
  42. private MainContentViewModel mainContentViewModel;
  43. public DelegateCommand OpenFileCommand { get; set; }
  44. public DelegateCommand<string> ShowToolCommand { get; set; }
  45. public DelegateCommand CreateBlackPDFCommand { get; set; }
  46. public DelegateCommand CreateFromOtherFile { get; set; }
  47. public DelegateCommand CreateFromHtmlCommnd { get; set; }
  48. public DelegateCommand CreateFromScanner { get; set; }
  49. public IRegionManager toolregion;
  50. public IEventAggregator eventer;
  51. public IDialogService dialog;
  52. public HomeContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IDialogService dialogaware)
  53. {
  54. toolregion = regionManager;
  55. eventer = eventAggregator;
  56. dialog = dialogaware;
  57. ToolRegionName = RegionNames.ToolRegionName;
  58. OpenFileCommand = new DelegateCommand(OpenFile);
  59. ShowToolCommand = new DelegateCommand<string>(ShowToolContent);
  60. CreateBlackPDFCommand = new DelegateCommand(CreatBlankPDF);
  61. CreateFromOtherFile = new DelegateCommand(createFromOtherFile);
  62. CreateFromHtmlCommnd = new DelegateCommand(createFormHtml);
  63. CreateFromScanner = new DelegateCommand(createFromScanner);
  64. }
  65. /// <summary>
  66. /// 显示右侧不同的工具栏页面
  67. /// </summary>
  68. /// <param name="view"></param>
  69. public void ShowToolContent(string view)
  70. {
  71. toolregion.RequestNavigate(ToolRegionName, view);
  72. }
  73. /// <summary>
  74. /// 从其他格式文件创建PDF
  75. /// </summary>
  76. private async void createFromOtherFile()
  77. {
  78. string txt = Properties.Resources.txtex;
  79. string word = Properties.Resources.wordex;
  80. string ppt = Properties.Resources.pptex;
  81. string excel = Properties.Resources.excelex;
  82. string html = Properties.Resources.htmlex;
  83. string image = Properties.Resources.imageex;
  84. string allfiles = txt + word + excel + ppt + image+html;
  85. OpenFileDialog dialog = new OpenFileDialog();
  86. dialog.Multiselect = true;
  87. dialog.Filter = string.Format($"Files({allfiles.Replace(";",",")}|{allfiles})|" +
  88. $"Microsoft Office Word({word})|{word}|" +
  89. $"Microsoft Office Excel({excel})|{excel}|" +
  90. $"Microsoft Office PowerPoint({ppt})|{ppt}|" +
  91. $"Txt({txt})|{txt}|" +
  92. $"Picture({image})|{image}|" +
  93. $"Html({html})|{html}");
  94. if((bool)dialog.ShowDialog())
  95. {
  96. var fileList = dialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  97. if (fileList.Count <= 0)
  98. {
  99. IsLoading = Visibility.Collapsed;
  100. return;
  101. }
  102. IsLoading = Visibility.Visible;
  103. await Task.Delay(10);
  104. //在当前页签打开第一个文件
  105. var type = System.IO.Path.GetExtension(fileList[0]).ToLower();
  106. if(image.Contains(type))
  107. {
  108. //图片文件
  109. mainContentViewModel.CreateFile(fileList[0]);
  110. }
  111. else
  112. {
  113. await mainContentViewModel.CreateFileFromOffice(fileList[0]);
  114. }
  115. for (int i = 1; i < fileList.Count(); i++)
  116. {
  117. if (!App.OpenedFileList.Contains(fileList[i]))
  118. {
  119. App.mainWindowViewModel.AddTabItem(fileList[i]);
  120. await Task.Delay(50);
  121. }
  122. ToolMethod.SetFileThumbImg(fileList[i]);
  123. }
  124. IsLoading = Visibility.Collapsed;
  125. }
  126. }
  127. /// <summary>
  128. /// 从网页创建PDF文件
  129. /// </summary>
  130. private void createFormHtml()
  131. {
  132. dialog.ShowDialog(DialogNames.CreateFromHtmlDialog, async e =>{
  133. if(e.Result== ButtonResult.OK)
  134. {
  135. IsLoading = Visibility.Visible;
  136. var model = e.Parameters.GetValue<Model.Dialog.HomePageToolsDialogs.HtmlModel>(ParameterNames.DataModel);
  137. await mainContentViewModel.CreateFileFromOffice(model.FilePath,model.PageSize,model.Margin);
  138. IsLoading = Visibility.Collapsed;
  139. }
  140. });
  141. }
  142. /// <summary>
  143. /// 从扫描仪创建
  144. /// </summary>
  145. private void createFromScanner()
  146. {
  147. }
  148. /// <summary>
  149. /// 打开文件
  150. /// </summary>
  151. public async void OpenFile()
  152. {
  153. OpenFileDialog openFileDialog = new OpenFileDialog();
  154. openFileDialog.Filter = Properties.Resources.OpenDialogFilter;
  155. openFileDialog.Multiselect = true;
  156. if ((bool)openFileDialog.ShowDialog())
  157. {
  158. IsLoading = Visibility.Visible;
  159. await Task.Delay(3);
  160. if (openFileDialog.FileNames.Count() == 1)
  161. {
  162. if (App.OpenedFileList.Contains(openFileDialog.FileName))
  163. {
  164. App.mainWindowViewModel.SelectItem(openFileDialog.FileName);
  165. }
  166. else
  167. {
  168. mainContentViewModel.OpenFile(openFileDialog.FileName);
  169. }
  170. ToolMethod.SetFileThumbImg(openFileDialog.FileName);
  171. }
  172. else
  173. {
  174. var fileList = openFileDialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  175. if (fileList.Count <= 0)
  176. {
  177. App.mainWindowViewModel.SelectItem(openFileDialog.FileName);
  178. IsLoading = Visibility.Collapsed;
  179. return;
  180. }
  181. mainContentViewModel.OpenFile(fileList[0]);
  182. for (int i = 1; i < fileList.Count(); i++)
  183. {
  184. if (!App.OpenedFileList.Contains(fileList[i]))
  185. {
  186. App.mainWindowViewModel.AddTabItem(fileList[i]);
  187. }
  188. ToolMethod.SetFileThumbImg(fileList[i]);
  189. }
  190. }
  191. IsLoading = Visibility.Collapsed;
  192. }
  193. }
  194. /// <summary>
  195. /// 创建空白文档
  196. /// </summary>
  197. public void CreatBlankPDF()
  198. {
  199. mainContentViewModel.CreateFile();
  200. }
  201. #region Navigate
  202. public void OnNavigatedTo(NavigationContext navigationContext)
  203. {
  204. var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel;
  205. if (mainVM != null)
  206. {
  207. mainContentViewModel = mainVM;
  208. mainContentViewModel.homeContentViewModel = this;
  209. }
  210. toolregion.RequestNavigate(ToolRegionName, "Guid");
  211. }
  212. public bool IsNavigationTarget(NavigationContext navigationContext)
  213. {
  214. return true;
  215. }
  216. public void OnNavigatedFrom(NavigationContext navigationContext)
  217. {
  218. }
  219. #endregion
  220. }
  221. }