using Microsoft.Win32; using PDF_Master.CustomControl; using PDF_Master.EventAggregators; using PDF_Master.Helper; using PDF_Master.Model; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace PDF_Master.ViewModels { public class HomeContentViewModel : BindableBase, INavigationAware { #region 文案 private string T_openFiles; public string T_OpenFiles { get { return T_openFiles; } set { SetProperty(ref T_openFiles, value); } } private string T_createPDF; public string T_CreatePDF { get { return T_createPDF; } set { SetProperty(ref T_createPDF, value); } } private string T_createPDFToNew; public string T_CreatePDFToNew { get { return T_createPDFToNew; } set { SetProperty(ref T_createPDFToNew, value); } } private void InitString() { T_OpenFiles = App.HomePageLoader.GetString("Home_OpenFiles"); T_CreatePDF = App.HomePageLoader.GetString("Home_CreatePDF"); T_CreatePDFToNew = App.HomePageLoader.GetString("Home_CreateToNew"); } #endregion private string fileName = "Home"; public string FileName { get { return fileName; } set { SetProperty(ref fileName, value); } } private string regionName; public string ToolRegionName { get { return regionName; } set { SetProperty(ref regionName, value); } } private Visibility isLoading = Visibility.Collapsed; public Visibility IsLoading { get { return isLoading; } set { SetProperty(ref isLoading, value); } } private MainContentViewModel mainContentViewModel; public DelegateCommand OpenFileCommand { get; set; } public DelegateCommand ShowToolCommand { get; set; } public DelegateCommand CreateBlackPDFCommand { get; set; } public DelegateCommand CreateFromOtherFile { get; set; } public DelegateCommand CreateFromHtmlCommnd { get; set; } public DelegateCommand CreateFromScanner { get; set; } public IRegionManager toolregion; public IEventAggregator eventer; public IDialogService dialog; public HomeContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IDialogService dialogaware) { toolregion = regionManager; eventer = eventAggregator; dialog = dialogaware; ToolRegionName = RegionNames.ToolRegionName; OpenFileCommand = new DelegateCommand(OpenFile); ShowToolCommand = new DelegateCommand(ShowToolContent); CreateBlackPDFCommand = new DelegateCommand(CreatBlankPDF); CreateFromOtherFile = new DelegateCommand(createFromOtherFile); CreateFromHtmlCommnd = new DelegateCommand(createFormHtml); CreateFromScanner = new DelegateCommand(createFromScanner); InitString(); } /// /// 显示右侧不同的工具栏页面 /// /// public void ShowToolContent(string view) { toolregion.RequestNavigate(ToolRegionName, view); } /// /// 从外部拖拽打开文件 /// /// public async void AddFileFromDrag(List files) { string txt = Properties.Resources.txtex; string word = Properties.Resources.wordex; string ppt = Properties.Resources.pptex; string excel = Properties.Resources.excelex; string html = Properties.Resources.htmlex; string image = Properties.Resources.imageex; string allfiles = txt + word + excel + ppt + html; if (files.Count() == 1 && App.OpenedFileList.Contains(files[0])) { //单文件打开重复文件时 需要选中已打开的重复文件 App.mainWindowViewModel.SelectItem(files[0]); return; } IsLoading = Visibility.Visible; //先在当前页签打开第一个文档 var type = System.IO.Path.GetExtension(files[0]).ToLower(); if (image.Contains(type)) { //从图片创建 mainContentViewModel.CreateFile(files[0]); } else if (type == ".pdf") { mainContentViewModel.OpenFile(files[0]); } else if(allfiles.Contains(files[0])) { await mainContentViewModel.CreateFileFromOffice(files[0]); } else { AlertsMessage alertsMessage = new AlertsMessage(); alertsMessage.ShowDialog("", "Unsupported file format", "OK"); IsLoading = Visibility.Collapsed; return; } ToolMethod.SetFileThumbImg(files[0]); //循环用新页签打开新文档 for (int i = 1; i < files.Count(); i++) { if (!App.OpenedFileList.Contains(files[i])) { App.mainWindowViewModel.AddTabItem(files[i]); await Task.Delay(50); } ToolMethod.SetFileThumbImg(files[i]); } IsLoading = Visibility.Collapsed; } /// /// 从其他格式文件创建PDF /// private async void createFromOtherFile() { string txt = Properties.Resources.txtex; string word = Properties.Resources.wordex; string ppt = Properties.Resources.pptex; string excel = Properties.Resources.excelex; string html = Properties.Resources.htmlex; string image = Properties.Resources.imageex; string allfiles = txt + word + excel + ppt + image + html; OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true; dialog.Filter = string.Format($"Files({allfiles.Replace(";", ",")}|{allfiles})|" + $"Microsoft Office Word({word})|{word}|" + $"Microsoft Office Excel({excel})|{excel}|" + $"Microsoft Office PowerPoint({ppt})|{ppt}|" + $"Txt({txt})|{txt}|" + $"Picture({image})|{image}|" + $"Html({html})|{html}"); if ((bool)dialog.ShowDialog()) { var fileList = dialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList(); if (fileList.Count <= 0) { IsLoading = Visibility.Collapsed; return; } IsLoading = Visibility.Visible; await Task.Delay(10); //在当前页签打开第一个文件 var type = System.IO.Path.GetExtension(fileList[0]).ToLower(); if (image.Contains(type)) { //图片文件 mainContentViewModel.CreateFile(fileList[0]); } else { await mainContentViewModel.CreateFileFromOffice(fileList[0]); } ToolMethod.SetFileThumbImg(fileList[0]); for (int i = 1; i < fileList.Count(); i++) { if (!App.OpenedFileList.Contains(fileList[i])) { App.mainWindowViewModel.AddTabItem(fileList[i]); await Task.Delay(50); } ToolMethod.SetFileThumbImg(fileList[i]); } IsLoading = Visibility.Collapsed; } } /// /// 从网页创建PDF文件 /// private void createFormHtml() { dialog.ShowDialog(DialogNames.CreateFromHtmlDialog, async e => { if (e.Result == ButtonResult.OK) { IsLoading = Visibility.Visible; var model = e.Parameters.GetValue(ParameterNames.DataModel); await mainContentViewModel.CreateFileFromOffice(model.FilePath, model.PageSize, model.Margin); IsLoading = Visibility.Collapsed; } }); } /// /// 从扫描仪创建 /// private void createFromScanner() { dialog.ShowDialog(DialogNames.CreateFromScannerDialogs, async e => { if (e.Result == ButtonResult.OK) { IsLoading = Visibility.Visible; var model = e.Parameters.GetValue(ParameterNames.DataModel); mainContentViewModel.CreateFile(model); IsLoading = Visibility.Collapsed; } }); } /// /// 打开文件 /// public async void OpenFile() { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = Properties.Resources.OpenDialogFilter; openFileDialog.Multiselect = true; if ((bool)openFileDialog.ShowDialog()) { IsLoading = Visibility.Visible; await Task.Delay(3); if (openFileDialog.FileNames.Count() == 1) { //单文件打开重复文件时 需要选中已打开的重复文件 if (App.OpenedFileList.Contains(openFileDialog.FileName)) { App.mainWindowViewModel.SelectItem(openFileDialog.FileName); } else { mainContentViewModel.OpenFile(openFileDialog.FileName); } ToolMethod.SetFileThumbImg(openFileDialog.FileName); } else { var fileList = openFileDialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList(); if (fileList.Count <= 0) { IsLoading = Visibility.Collapsed; return; } mainContentViewModel.OpenFile(fileList[0]); for (int i = 1; i < fileList.Count(); i++) { if (!App.OpenedFileList.Contains(fileList[i])) { App.mainWindowViewModel.AddTabItem(fileList[i]); } ToolMethod.SetFileThumbImg(fileList[i]); } } IsLoading = Visibility.Collapsed; } } /// /// 创建空白文档 /// public void CreatBlankPDF() { mainContentViewModel.CreateFile(); } #region Navigate public void OnNavigatedTo(NavigationContext navigationContext) { var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel; if (mainVM != null) { mainContentViewModel = mainVM; mainContentViewModel.homeContentViewModel = this; } toolregion.RequestNavigate(ToolRegionName, "Guid"); } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } #endregion } }