HomeContentViewModel.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. return;
  103. IsLoading = Visibility.Visible;
  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. //需要加一定延时 不然连续添加多个文件时会出现regionname重名的情况 因为多处导航是异步导航,可能绑定还没更新
  120. await Task.Delay(20);
  121. App.mainWindowViewModel.AddTabItem(fileList[i]);
  122. }
  123. ToolMethod.SetFileThumbImg(fileList[i]);
  124. }
  125. IsLoading = Visibility.Collapsed;
  126. }
  127. }
  128. /// <summary>
  129. /// 从网页创建PDF文件
  130. /// </summary>
  131. private void createFormHtml()
  132. {
  133. dialog.ShowDialog(DialogNames.CreateFromHtmlDialog, async e =>{
  134. if(e.Result== ButtonResult.OK)
  135. {
  136. IsLoading = Visibility.Visible;
  137. var model = e.Parameters.GetValue<Model.Dialog.HomePageToolsDialogs.HtmlModel>(ParameterNames.DataModel);
  138. await mainContentViewModel.CreateFileFromOffice(model.FilePath,model.PageSize,model.Margin);
  139. IsLoading = Visibility.Collapsed;
  140. }
  141. });
  142. }
  143. /// <summary>
  144. /// 从扫描仪创建
  145. /// </summary>
  146. private void createFromScanner()
  147. {
  148. }
  149. /// <summary>
  150. /// 打开文件
  151. /// </summary>
  152. public async void OpenFile()
  153. {
  154. OpenFileDialog openFileDialog = new OpenFileDialog();
  155. openFileDialog.Filter = Properties.Resources.OpenDialogFilter;
  156. openFileDialog.Multiselect = true;
  157. if ((bool)openFileDialog.ShowDialog())
  158. {
  159. IsLoading = Visibility.Visible;
  160. await Task.Delay(3);
  161. if (openFileDialog.FileNames.Count() == 1)
  162. {
  163. if (App.OpenedFileList.Contains(openFileDialog.FileName))
  164. {
  165. App.mainWindowViewModel.SelectItem(openFileDialog.FileName);
  166. }
  167. else
  168. {
  169. mainContentViewModel.OpenFile(openFileDialog.FileName);
  170. }
  171. ToolMethod.SetFileThumbImg(openFileDialog.FileName);
  172. }
  173. else
  174. {
  175. var fileList = openFileDialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  176. if (fileList.Count <= 0)
  177. return;
  178. mainContentViewModel.OpenFile(fileList[0]);
  179. for (int i = 1; i < fileList.Count(); i++)
  180. {
  181. if (!App.OpenedFileList.Contains(fileList[i]))
  182. {
  183. //需要加一定延时 不然连续添加多个文件时会出现regionname重名的情况 因为多处导航是异步导航,可能绑定还没更新
  184. await Task.Delay(20);
  185. App.mainWindowViewModel.AddTabItem(fileList[i]);
  186. }
  187. ToolMethod.SetFileThumbImg(fileList[i]);
  188. }
  189. }
  190. IsLoading = Visibility.Collapsed;
  191. }
  192. }
  193. /// <summary>
  194. /// 创建空白文档
  195. /// </summary>
  196. public void CreatBlankPDF()
  197. {
  198. mainContentViewModel.CreateFile();
  199. }
  200. #region Navigate
  201. public void OnNavigatedTo(NavigationContext navigationContext)
  202. {
  203. var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel;
  204. if (mainVM != null)
  205. {
  206. mainContentViewModel = mainVM;
  207. }
  208. }
  209. public bool IsNavigationTarget(NavigationContext navigationContext)
  210. {
  211. return true;
  212. }
  213. public void OnNavigatedFrom(NavigationContext navigationContext)
  214. {
  215. }
  216. #endregion
  217. }
  218. }