MainContentViewModel.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. using ComPDFKitViewer.PdfViewer;
  2. using Microsoft.Win32;
  3. using PDF_Master.CustomControl;
  4. using PDF_Master.EventAggregators;
  5. using PDF_Master.Views;
  6. using Prism.Commands;
  7. using Prism.Events;
  8. using Prism.Ioc;
  9. using Prism.Mvvm;
  10. using Prism.Regions;
  11. using Prism.Services.Dialogs;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Diagnostics;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using PDF_Master.Model;
  20. using System.ComponentModel;
  21. using PDF_Master.Helper;
  22. using PDFSettings.Settings;
  23. using System.Drawing;
  24. using System.IO;
  25. using System.Drawing.Imaging;
  26. using ComPDFKit.PDFDocument;
  27. using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
  28. using PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs;
  29. using PDF_Master.Properties;
  30. using WIA;
  31. namespace PDF_Master.ViewModels
  32. {
  33. public class MainContentViewModel : BindableBase, INavigationAware
  34. {
  35. #region 文案
  36. private string T_rename;
  37. public string T_Rename
  38. {
  39. get { return T_rename; }
  40. set
  41. {
  42. SetProperty(ref T_rename, value);
  43. }
  44. }
  45. private string T_floder;
  46. public string T_Floder
  47. {
  48. get { return T_floder; }
  49. set
  50. {
  51. SetProperty(ref T_floder, value);
  52. }
  53. }
  54. private string T_close;
  55. public string T_Close
  56. {
  57. get { return T_close; }
  58. set
  59. {
  60. SetProperty(ref T_close, value);
  61. }
  62. }
  63. private string T_closeAll;
  64. public string T_CloseAll
  65. {
  66. get { return T_closeAll; }
  67. set
  68. {
  69. SetProperty(ref T_closeAll, value);
  70. }
  71. }
  72. private void InitString()
  73. {
  74. FileName = App.MainPageLoader.GetString("MultipleTabs_Default");
  75. T_Rename = App.MainPageLoader.GetString("MultipleTabs_Rename");
  76. T_Floder = App.MainPageLoader.GetString("MultipleTabs_ Folder");
  77. T_Close = App.MainPageLoader.GetString("MultipleTabs_ Close");
  78. T_CloseAll = App.MainPageLoader.GetString("MultipleTabs_ CloseAll");
  79. }
  80. #endregion
  81. /// <summary>
  82. /// 是否处于阅读模式
  83. /// </summary>
  84. public bool IsBookMode = false;
  85. private string fileName = "Home";
  86. public string FileName
  87. {
  88. get { return fileName; }
  89. set
  90. {
  91. SetProperty(ref fileName, value);
  92. if (IsReNameTextShow == Visibility.Visible)
  93. {
  94. dorenameFile();
  95. }
  96. }
  97. }
  98. private string filePath;
  99. public string FilePath
  100. {
  101. get { return filePath; }
  102. set
  103. {
  104. SetProperty(ref filePath, value);
  105. if (!string.IsNullOrEmpty(filePath))
  106. {
  107. FileName = System.IO.Path.GetFileName(filePath);
  108. }
  109. }
  110. }
  111. private Visibility fileChanged = Visibility.Collapsed;
  112. public Visibility FileChanged
  113. {
  114. get { return fileChanged; }
  115. set { SetProperty(ref fileChanged, value); }
  116. }
  117. public CPDFViewer PDFViewer { get; set; }
  118. public DelegateCommand<object> CloseTab { get; set; }
  119. public DelegateCommand UnLoaded { get; set; }
  120. public DelegateCommand ShowInFolderCommand { get; set; }
  121. public DelegateCommand RenameCommand { get; set; }
  122. public ViewContentViewModel viewContentViewModel { get; set; }
  123. public MainWindowViewModel mainWindowViewModel { get; set; }
  124. public HomeContentViewModel homeContentViewModel { get; set; }
  125. private string regionName;
  126. public string MainContentRegionName
  127. {
  128. get { return regionName; }
  129. set { SetProperty(ref regionName, value); }
  130. }
  131. private bool isReNameEnable = false;
  132. /// <summary>
  133. /// 是否能够重命名
  134. /// </summary>
  135. public bool IsReNameEnable
  136. {
  137. get { return isReNameEnable; }
  138. set
  139. {
  140. SetProperty(ref isReNameEnable, value);
  141. }
  142. }
  143. private bool isShowInFolderEnable = false;
  144. /// <summary>
  145. /// 是否能够显示文件浏览器
  146. /// </summary>
  147. public bool IsShowInFolderEnable
  148. {
  149. get { return isShowInFolderEnable; }
  150. set
  151. {
  152. SetProperty(ref isShowInFolderEnable, value);
  153. }
  154. }
  155. private Visibility isRenameTextShow = Visibility.Collapsed;
  156. public Visibility IsReNameTextShow
  157. {
  158. get { return isRenameTextShow; }
  159. set
  160. {
  161. SetProperty(ref isRenameTextShow, value);
  162. }
  163. }
  164. private bool newFile = false;
  165. /// <summary>
  166. /// 是否是新建的文档
  167. /// </summary>
  168. public bool NewFile
  169. {
  170. get { return newFile; }
  171. set
  172. {
  173. SetProperty(ref newFile, value);
  174. if(!value)
  175. {
  176. IsShowInFolderEnable = true;
  177. }
  178. }
  179. }
  180. public IRegionManager toolregion;
  181. public IEventAggregator eventer;
  182. public IContainerProvider container;
  183. public IDialogService dialogs;
  184. public MainContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IContainerProvider containerProvider, IDialogService dialogService)
  185. {
  186. toolregion = regionManager;
  187. eventer = eventAggregator;
  188. container = containerProvider;
  189. dialogs = dialogService;
  190. CloseTab = new DelegateCommand<object>(CloseTabItem);
  191. ShowInFolderCommand = new DelegateCommand(showInFolder);
  192. RenameCommand = new DelegateCommand(rename);
  193. UnLoaded = new DelegateCommand(unload);
  194. MainContentRegionName = Guid.NewGuid().ToString();
  195. InitString();
  196. }
  197. //关闭页签
  198. //清除文件记录,释放Document对象
  199. private void unload()
  200. {
  201. if (PDFViewer != null)
  202. {
  203. App.OpenedFileList.Remove(PDFViewer.Document?.FilePath);
  204. PDFViewer.CloseDocument();
  205. }
  206. }
  207. /// <summary>
  208. /// 执行重命名操作
  209. /// </summary>
  210. private void dorenameFile()
  211. {
  212. var folder = PDFViewer.Document.FilePath.Substring(0, PDFViewer.Document.FilePath.LastIndexOf("\\"));
  213. ////File.Replace(PDFViewer.Document.FilePath,Path.Combine(folder,FileName),Path.Combine(folder,"back.bak"),true);
  214. ///TODO:会被占用 无法操作
  215. IsReNameTextShow = Visibility.Collapsed;
  216. }
  217. /// <summary>
  218. /// 显示重命名文本框
  219. /// </summary>
  220. private void rename()
  221. {
  222. IsReNameTextShow = Visibility.Visible;
  223. }
  224. private void showInFolder()
  225. {
  226. if (!string.IsNullOrEmpty(PDFViewer.Document.FilePath))
  227. {
  228. CommonHelper.ShowFileBrowser(PDFViewer.Document.FilePath);
  229. }
  230. }
  231. private void CloseTabItem(object item)
  232. {
  233. if (item == null)
  234. {
  235. App.mainWindowViewModel?.CloseTabItem(mainWindowViewModel.SelectedItem);
  236. }
  237. else
  238. {
  239. App.mainWindowViewModel?.CloseTabItem(item);
  240. App.OpenedFileList.Remove(FilePath);
  241. if(NewFile)
  242. {
  243. //新文档 关闭文档后 及时清空临时缓存
  244. DirectoryInfo directory = new DirectoryInfo(CacheFilePath.Instance.CreatedFilePath);
  245. FileInfo fileInfo = new FileInfo(FilePath);
  246. if(directory.GetFiles().ToList().Contains(fileInfo))
  247. {
  248. File.Delete(FilePath);
  249. }
  250. }
  251. }
  252. }
  253. /// <summary>
  254. /// 打开指定路径的PDF文件
  255. /// </summary>
  256. /// <param name="filePath"></param>
  257. public void OpenFile(string filePath, PasswordInfo passwordInfo = null)
  258. {
  259. var result = LoadFileFormPath(filePath);
  260. if (!result)
  261. {
  262. return;
  263. }
  264. FilePath = filePath;
  265. NavigateToViewContent();
  266. //检查是否是新文档
  267. OpenFileInfo isnew = SettingHelper.GetFileInfo(filePath);
  268. if (isnew == null)
  269. {
  270. if (App.OpenedFileList.Contains(filePath) == false)
  271. {
  272. App.OpenedFileList.Add(filePath);
  273. }
  274. }
  275. //打开文件后,不管是新文件还是旧文件都需要更新排序
  276. SettingHelper.SortRecentOpenFiles(filePath);
  277. }
  278. /// <summary>
  279. /// 创建PDFviewer对象后导航到ViewContent
  280. /// </summary>
  281. private void NavigateToViewContent(PasswordInfo passwordInfo = null)
  282. {
  283. NavigationParameters parameters = new NavigationParameters {
  284. { ParameterNames.MainViewModel, this },
  285. { ParameterNames.PDFViewer,PDFViewer},
  286. { ParameterNames.PasswordInfo, passwordInfo}
  287. };
  288. toolregion.RequestNavigate(MainContentRegionName, "ViewContent", parameters);
  289. IsReNameEnable = true;
  290. if (!string.IsNullOrEmpty(PDFViewer.Document.FilePath)&&!NewFile)
  291. {
  292. IsShowInFolderEnable = true;
  293. }
  294. }
  295. /// <summary>
  296. /// 从文件路径创建PDFViewer对象,已包含文档解密逻辑
  297. /// </summary>
  298. /// <param name="path"></param>
  299. /// <returns></returns>
  300. private bool LoadFileFormPath(string path)
  301. {
  302. PDFViewer = new CPDFViewer();
  303. PDFViewer.InitDocument(path);
  304. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  305. if (PDFViewer.Document == null)
  306. {
  307. //MessageBoxEx.Show(App.MainPageLoader.GetString("Main_OpenFileFailedWarning"));
  308. return false;
  309. }
  310. else
  311. {
  312. //解密逻辑调整到显示阅读页后,测试确认没问题后,删除下面的注释部分代码
  313. //VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusOpenPassword, dialogs);
  314. //if (result.IsDiscryptied)
  315. //{
  316. // if (result.Password != null)
  317. // {
  318. // string filePath = PDFViewer.Document.FilePath;
  319. // PDFViewer.Document.UnlockWithPassword(result.Password);
  320. // PDFViewer.Tag = result.Password;
  321. // }
  322. // ///TODO:
  323. // ///此处填入需要执行的代码
  324. //}
  325. //if (result.IsDiscryptied == false)
  326. //{
  327. // //未成功解密文档时,释放Document对象,返回
  328. // PDFViewer.Document.Release();
  329. // return false;
  330. //}
  331. }
  332. //PDFViewer.Load();
  333. //PDFViewer.SetFormFieldHighlight(Settings.Default.AppProperties.InitialVIew.HighlightLink);
  334. //PDFViewer.SetBackgroundBrush(new System.Windows.Media.SolidColorBrush(Settings.Default.AppProperties.InitialVIew.BackGround));
  335. if (App.mainWindowViewModel != null)
  336. {
  337. App.mainWindowViewModel.CurrentPDFViewer = PDFViewer;
  338. }
  339. return true;
  340. }
  341. /// <summary>
  342. /// 创建文件,路径为空时表示创建空白文档
  343. /// 否则表示从图片路径创建PDf
  344. /// </summary>
  345. /// <param name="imagePath"></param>
  346. /// <returns></returns>
  347. public bool CreateFile(string imagePath = null)
  348. {
  349. PDFViewer = new CPDFViewer();
  350. PDFViewer.CreateDocument();
  351. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  352. if (PDFViewer.Document == null)
  353. {
  354. AlertsMessage alertsMessage = new AlertsMessage();
  355. alertsMessage.ShowDialog("", "创建文件失败.", "OK");
  356. return false;
  357. }
  358. if (string.IsNullOrEmpty(imagePath))
  359. {
  360. FileName = "Blank Page.pdf";
  361. //默认插入595*842 大小的页面
  362. PDFViewer.Document.InsertPage(0, 595, 842, null);
  363. }
  364. else
  365. {
  366. FileName = imagePath.Substring(imagePath.LastIndexOf("\\") + 1, imagePath.LastIndexOf(".") - imagePath.LastIndexOf("\\") - 1) + ".pdf";
  367. Bitmap pic = new Bitmap(imagePath);
  368. int width = pic.Size.Width;
  369. int height = pic.Size.Height;
  370. string ex = System.IO.Path.GetExtension(imagePath);
  371. //其他格式或者名称中含空格的图片 在本地先转存一下
  372. if ((ex != ".jpg" && ex != ".jpeg") || !Uri.IsWellFormedUriString(imagePath, UriKind.Absolute))
  373. {
  374. //将其他格式图片转换成jpg
  375. string folderPath = Path.GetTempPath();
  376. if (File.Exists(folderPath))
  377. {
  378. File.Delete(folderPath);
  379. }
  380. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  381. if (!tempfolder.Exists)
  382. {
  383. tempfolder.Create();
  384. }
  385. string targetPath = System.IO.Path.Combine(folderPath, Guid.NewGuid().ToString());
  386. imagePath = targetPath;
  387. pic.Save(targetPath, ImageFormat.Jpeg);
  388. }
  389. pic.Dispose();
  390. var result = PDFViewer.Document.InsertPage(0, width, height, imagePath);
  391. if (!result)
  392. {
  393. AlertsMessage alertsMessage = new AlertsMessage();
  394. alertsMessage.ShowDialog("", "创建文件失败.", "OK");
  395. return false;
  396. }
  397. }
  398. SaveToTempFile();
  399. PDFViewer.UndoManager.CanSave = true;
  400. FileChanged = Visibility.Visible;
  401. NavigateToViewContent();
  402. return true;
  403. }
  404. /// <summary>
  405. /// 从office文件转换成PDF
  406. /// </summary>
  407. /// <param name="sourcepath"></param>
  408. /// <returns></returns>
  409. public async Task<bool> CreateFileFromOffice(string sourcepath, Microsoft.Office.Interop.Word.WdPaperSize paperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4, double margin = 0)
  410. {
  411. try
  412. {
  413. //生成存放临时pdf的临时文件夹
  414. string folderPath = Path.GetTempPath();
  415. if (File.Exists(folderPath))
  416. {
  417. File.Delete(folderPath);
  418. }
  419. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  420. if (!tempfolder.Exists)
  421. {
  422. tempfolder.Create();
  423. }
  424. string targetPath = System.IO.Path.Combine(folderPath, Guid.NewGuid().ToString() + ".pdf");
  425. string ex = System.IO.Path.GetExtension(sourcepath).ToLower();
  426. await Task.Delay(10);
  427. switch (ex)
  428. {
  429. case ".doc":
  430. case ".docx":
  431. case "docm":
  432. case ".dot":
  433. case ".dotx":
  434. case ".dotm":
  435. case ".txt":
  436. case ".html":
  437. await Task.Run(() =>
  438. {
  439. Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
  440. Microsoft.Office.Interop.Word.Document document = null;
  441. word.Visible = false;
  442. word.ShowWindowsInTaskbar = true;
  443. document = word.Documents.Open(sourcepath);
  444. var page = document.PageSetup;
  445. page.PaperSize = paperSize;
  446. if (margin > 0)
  447. {
  448. page.LeftMargin = page.TopMargin = page.RightMargin = page.BottomMargin = (float)margin;
  449. }
  450. document?.ExportAsFixedFormat(targetPath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
  451. document?.Close(false);
  452. if (word != null)
  453. {
  454. word.NormalTemplate.Saved = true;
  455. word.Quit();
  456. }
  457. });
  458. break;
  459. case ".xls":
  460. case ".xlsx":
  461. case ".xlsm":
  462. case ".xlsb":
  463. case ".xlam":
  464. case ".xltx":
  465. case ".xlt":
  466. await Task.Run(() =>
  467. {
  468. Microsoft.Office.Interop.Excel.Application excele = new Microsoft.Office.Interop.Excel.Application();
  469. Microsoft.Office.Interop.Excel.Workbook workbook = null;
  470. excele.Visible = false;
  471. try
  472. {
  473. workbook = excele.Workbooks.Open(sourcepath);
  474. }
  475. catch (Exception e)
  476. {
  477. workbook = excele.Workbooks.Open(sourcepath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "t", false, false, 0, true, 1, Microsoft.Office.Interop.Excel.XlCorruptLoad.xlRepairFile);
  478. }
  479. workbook?.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, targetPath);
  480. workbook?.Close();
  481. excele?.Quit();
  482. });
  483. break;
  484. case ".ppt":
  485. case ".pptx":
  486. case ".pptm":
  487. case ".pptsx":
  488. case ".pps":
  489. case ".pptsm":
  490. case ".pot":
  491. case ".potm":
  492. await Task.Run(() =>
  493. {
  494. Microsoft.Office.Interop.PowerPoint.Application ppt = new Microsoft.Office.Interop.PowerPoint.Application();
  495. Microsoft.Office.Interop.PowerPoint.Presentation presentation = null;
  496. ppt.Visible = Microsoft.Office.Core.MsoTriState.msoCTrue;
  497. presentation = ppt.Presentations.Open(sourcepath);
  498. presentation.ExportAsFixedFormat(targetPath, Microsoft.Office.Interop.PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF);
  499. presentation?.Close();
  500. ppt?.Quit();
  501. });
  502. break;
  503. }
  504. PDFViewer = new CPDFViewer();
  505. PDFViewer.CreateDocument();
  506. if (PDFViewer.Document == null)
  507. {
  508. AlertsMessage alertsMessage = new AlertsMessage();
  509. alertsMessage.ShowDialog("", "创建PDF失败", "OK");
  510. return false;
  511. }
  512. FileName = sourcepath.Substring(sourcepath.LastIndexOf("\\") + 1, sourcepath.LastIndexOf(".") - sourcepath.LastIndexOf("\\") - 1) + ".pdf";
  513. var tempdoc = CPDFDocument.InitWithFilePath(targetPath);
  514. if (tempdoc == null)
  515. {
  516. AlertsMessage alertsMessage = new AlertsMessage();
  517. alertsMessage.ShowDialog("", "创建PDF失败", "OK");
  518. return false;
  519. }
  520. PDFViewer.Document.ImportPages(tempdoc, "");
  521. SaveToTempFile();
  522. PDFViewer.UndoManager.CanSave = true;
  523. FileChanged = Visibility.Visible;
  524. await System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  525. {
  526. NavigateToViewContent();
  527. }));
  528. return true;
  529. }
  530. catch (Exception ex)
  531. {
  532. string str = "没有安装";
  533. //判断是否有安装office软件
  534. var officeType = Type.GetTypeFromProgID("Word.Application");
  535. if (officeType == null)
  536. {
  537. str += "Word;";
  538. }
  539. officeType = Type.GetTypeFromProgID("Excel.Application");
  540. if (officeType == null)
  541. {
  542. str += "Excel;";
  543. }
  544. officeType = Type.GetTypeFromProgID("Powerpoint.Application");
  545. if (officeType == null)
  546. {
  547. str += "Excel;";
  548. }
  549. AlertsMessage alertsMessage = new AlertsMessage();
  550. if (str != "没有安装")
  551. {
  552. alertsMessage.ShowDialog("", "没有安装Office", "OK");
  553. }
  554. else
  555. {
  556. alertsMessage.ShowDialog("", "创建PDF失败", "OK");
  557. }
  558. return false;
  559. }
  560. }
  561. /// <summary>
  562. /// 新建的文档 保存至临时文件 解决新建文档没有路径的问题
  563. /// </summary>
  564. private void SaveToTempFile()
  565. {
  566. if(PDFViewer!=null)
  567. {
  568. string tempfile = CacheFilePath.Instance.CreatedFilePath;
  569. string file = CommonHelper.CreateFilePath(Path.Combine(tempfile,"Untitle.pdf"));
  570. PDFViewer.Document.WriteToFilePath(file);
  571. PDFViewer.CloseDocument();
  572. PDFViewer.InitDocument(file);
  573. FilePath = file;
  574. //在viewcontent里打开是 会重新load 因此这里不需要load 避免重复
  575. FileName = PDFViewer.Document.FileName;
  576. NewFile = true;
  577. }
  578. }
  579. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  580. {
  581. if (e.PropertyName == "CanSave")
  582. {
  583. if (PDFViewer.UndoManager.CanSave)
  584. {
  585. FileChanged = Visibility.Visible;
  586. }
  587. else
  588. {
  589. FileChanged = Visibility.Collapsed;
  590. }
  591. if (!string.IsNullOrEmpty(PDFViewer.Document.FileName))
  592. {
  593. FileName = PDFViewer.Document.FileName;
  594. }
  595. }
  596. }
  597. #region Navigation
  598. public async void OnNavigatedTo(NavigationContext navigationContext)
  599. {
  600. mainWindowViewModel = App.mainWindowViewModel;
  601. //因为是异步打开多个文件,需要先显示Home界面
  602. NavigationParameters parameters = new NavigationParameters();
  603. parameters.Add(ParameterNames.MainViewModel, this);
  604. if (toolregion.Regions.ContainsRegionWithName(MainContentRegionName))
  605. {
  606. toolregion.RequestNavigate(MainContentRegionName, "HomeContent", parameters);
  607. }
  608. //常规加载首页的情况
  609. if (navigationContext.Parameters.Count <= 0)
  610. {
  611. return;
  612. }
  613. //一次打开多个文件的情况
  614. var filepath = navigationContext.Parameters[ParameterNames.FilePath].ToString();
  615. if (filepath != null)
  616. {
  617. bool result = true;
  618. if (System.IO.Path.GetExtension(filepath.ToString()).ToLower() == ".pdf")
  619. {
  620. OpenFile(filepath.ToString());
  621. }
  622. else if (Properties.Resources.imageex.Contains(System.IO.Path.GetExtension(filepath).ToLower()))
  623. {
  624. //图片转PDF
  625. result = CreateFile(filepath);
  626. }
  627. else
  628. {
  629. result = await CreateFileFromOffice(filepath);
  630. }
  631. if (!result)
  632. {
  633. mainWindowViewModel.CloseTabItem(mainWindowViewModel.SelectedItem);
  634. return;
  635. }
  636. if (!App.OpenedFileList.Contains(filepath))
  637. {
  638. //更新已打开的文件记录
  639. App.OpenedFileList.Add(filepath);
  640. }
  641. }
  642. }
  643. public bool IsNavigationTarget(NavigationContext navigationContext)
  644. {
  645. return false;
  646. }
  647. public void OnNavigatedFrom(NavigationContext navigationContext)
  648. {
  649. }
  650. #endregion Navigation
  651. }
  652. }