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. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  65. {
  66. toolregion.RequestNavigate(ToolRegionName, "Guid");
  67. }));
  68. }
  69. /// <summary>
  70. /// 显示右侧不同的工具栏页面
  71. /// </summary>
  72. /// <param name="view"></param>
  73. public void ShowToolContent(string view)
  74. {
  75. toolregion.RequestNavigate(ToolRegionName, view);
  76. }
  77. /// <summary>
  78. /// 从其他格式文件创建PDF
  79. /// </summary>
  80. private async void createFromOtherFile()
  81. {
  82. string txt = Properties.Resources.txtex;
  83. string word = Properties.Resources.wordex;
  84. string ppt = Properties.Resources.pptex;
  85. string excel = Properties.Resources.excelex;
  86. string html = Properties.Resources.htmlex;
  87. string image = Properties.Resources.imageex;
  88. string allfiles = txt + word + excel + ppt + image+html;
  89. OpenFileDialog dialog = new OpenFileDialog();
  90. dialog.Multiselect = true;
  91. dialog.Filter = string.Format($"Files({allfiles.Replace(";",",")}|{allfiles})|" +
  92. $"Microsoft Office Word({word})|{word}|" +
  93. $"Microsoft Office Excel({excel})|{excel}|" +
  94. $"Microsoft Office PowerPoint({ppt})|{ppt}|" +
  95. $"Txt({txt})|{txt}|" +
  96. $"Picture({image})|{image}|" +
  97. $"Html({html})|{html}");
  98. if((bool)dialog.ShowDialog())
  99. {
  100. var fileList = dialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  101. if (fileList.Count <= 0)
  102. {
  103. IsLoading = Visibility.Collapsed;
  104. return;
  105. }
  106. IsLoading = Visibility.Visible;
  107. //在当前页签打开第一个文件
  108. var type = System.IO.Path.GetExtension(fileList[0]).ToLower();
  109. if(image.Contains(type))
  110. {
  111. //图片文件
  112. mainContentViewModel.CreateFile(fileList[0]);
  113. }
  114. else
  115. {
  116. await mainContentViewModel.CreateFileFromOffice(fileList[0]);
  117. }
  118. for (int i = 1; i < fileList.Count(); i++)
  119. {
  120. if (!App.OpenedFileList.Contains(fileList[i]))
  121. {
  122. App.mainWindowViewModel.AddTabItem(fileList[i]);
  123. }
  124. ToolMethod.SetFileThumbImg(fileList[i]);
  125. }
  126. IsLoading = Visibility.Collapsed;
  127. }
  128. }
  129. /// <summary>
  130. /// 从网页创建PDF文件
  131. /// </summary>
  132. private void createFormHtml()
  133. {
  134. dialog.ShowDialog(DialogNames.CreateFromHtmlDialog, async e =>{
  135. if(e.Result== ButtonResult.OK)
  136. {
  137. IsLoading = Visibility.Visible;
  138. var model = e.Parameters.GetValue<Model.Dialog.HomePageToolsDialogs.HtmlModel>(ParameterNames.DataModel);
  139. await mainContentViewModel.CreateFileFromOffice(model.FilePath,model.PageSize,model.Margin);
  140. IsLoading = Visibility.Collapsed;
  141. }
  142. });
  143. }
  144. /// <summary>
  145. /// 从扫描仪创建
  146. /// </summary>
  147. private void createFromScanner()
  148. {
  149. }
  150. /// <summary>
  151. /// 打开文件
  152. /// </summary>
  153. public async void OpenFile()
  154. {
  155. OpenFileDialog openFileDialog = new OpenFileDialog();
  156. openFileDialog.Filter = Properties.Resources.OpenDialogFilter;
  157. openFileDialog.Multiselect = true;
  158. if ((bool)openFileDialog.ShowDialog())
  159. {
  160. IsLoading = Visibility.Visible;
  161. await Task.Delay(3);
  162. if (openFileDialog.FileNames.Count() == 1)
  163. {
  164. if (App.OpenedFileList.Contains(openFileDialog.FileName))
  165. {
  166. App.mainWindowViewModel.SelectItem(openFileDialog.FileName);
  167. }
  168. else
  169. {
  170. mainContentViewModel.OpenFile(openFileDialog.FileName);
  171. }
  172. ToolMethod.SetFileThumbImg(openFileDialog.FileName);
  173. }
  174. else
  175. {
  176. var fileList = openFileDialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  177. if (fileList.Count <= 0)
  178. {
  179. App.mainWindowViewModel.SelectItem(openFileDialog.FileName);
  180. IsLoading = Visibility.Collapsed;
  181. return;
  182. }
  183. mainContentViewModel.OpenFile(fileList[0]);
  184. for (int i = 1; i < fileList.Count(); i++)
  185. {
  186. if (!App.OpenedFileList.Contains(fileList[i]))
  187. {
  188. App.mainWindowViewModel.AddTabItem(fileList[i]);
  189. }
  190. ToolMethod.SetFileThumbImg(fileList[i]);
  191. }
  192. }
  193. IsLoading = Visibility.Collapsed;
  194. }
  195. }
  196. /// <summary>
  197. /// 创建空白文档
  198. /// </summary>
  199. public void CreatBlankPDF()
  200. {
  201. mainContentViewModel.CreateFile();
  202. }
  203. #region Navigate
  204. public void OnNavigatedTo(NavigationContext navigationContext)
  205. {
  206. var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel;
  207. if (mainVM != null)
  208. {
  209. mainContentViewModel = mainVM;
  210. }
  211. }
  212. public bool IsNavigationTarget(NavigationContext navigationContext)
  213. {
  214. return true;
  215. }
  216. public void OnNavigatedFrom(NavigationContext navigationContext)
  217. {
  218. }
  219. #endregion
  220. }
  221. }