HomeContentViewModel.cs 10.0 KB

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