123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- 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;
- using WIA;
- 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<string> ShowToolCommand { get; set; }
- public DelegateCommand CreateBlackPDFCommand { get; set; }
- public DelegateCommand CreateFromOtherFile { get; set; }
- public DelegateCommand CreateFromHtmlCommnd { get; set; }
- public DelegateCommand<string> 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<string>(ShowToolContent);
- CreateBlackPDFCommand = new DelegateCommand(CreatBlankPDF);
- CreateFromOtherFile = new DelegateCommand(createFromOtherFile);
- CreateFromHtmlCommnd = new DelegateCommand(createFormHtml);
- CreateFromScanner = new DelegateCommand<string>(createFromScanner);
- InitString();
- }
- /// <summary>
- /// 显示右侧不同的工具栏页面
- /// </summary>
- /// <param name="view"></param>
- public void ShowToolContent(string view)
- {
- toolregion.RequestNavigate(ToolRegionName, view);
- }
- /// <summary>
- /// 从外部拖拽打开文件
- /// </summary>
- /// <param name="files"></param>
- public async void AddFileFromDrag(List<string> 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;
- await Task.Delay(3);
- //排除文件夹或其他类型文件
- if (!System.IO.File.Exists(files[0]))
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", App.MainPageLoader.GetString("ErrorFile_Warning"), App.ServiceLoader.GetString("Text_ok"));
- IsLoading = Visibility.Collapsed;
- return;
- }
- #region 先在当前页签打开第一个文档
- 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(type))
- {
- mainContentViewModel.CreateFileFromOffice(files[0]);
- }
- else
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", App.MainPageLoader.GetString("ErrorFile_Warning"), App.ServiceLoader.GetString("Text_ok"));
- IsLoading = Visibility.Collapsed;
- return;
- }
- ToolMethod.SetFileThumbImg(files[0]);
- #endregion
- #region 循环用新页签打开新文档
- 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]);
- }
- #endregion
- IsLoading = Visibility.Collapsed;
- }
- /// <summary>
- /// 从其他格式文件创建PDF
- /// </summary>
- 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
- {
- 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;
- }
- }
- /// <summary>
- /// 从网页创建PDF文件
- /// </summary>
- private void createFormHtml()
- {
- dialog.ShowDialog(DialogNames.CreateFromHtmlDialog, async e =>
- {
- if (e.Result == ButtonResult.OK)
- {
- IsLoading = Visibility.Visible;
- var model = e.Parameters.GetValue<Model.Dialog.HomePageToolsDialogs.HtmlModel>(ParameterNames.DataModel);
- mainContentViewModel.CreateFileFromOffice(model.FilePath, model.PageSize, model.Margin);
- IsLoading = Visibility.Collapsed;
- }
- });
- }
- /// <summary>
- /// 从扫描仪创建
- /// </summary>
- private async void createFromScanner(string args)
- {
- dialog.ShowDialog(DialogNames.CreateFromScannerDialogs, async e =>
- {
- if (e.Result == ButtonResult.OK)
- {
- IsLoading = Visibility.Visible;
- string path;
- e.Parameters.TryGetValue("Imagepath", out path);
- if (args == "View")
- {
- App.mainWindowViewModel.AddTab.Execute();
- await Task.Delay(30);
- (App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel).CreateFile();
- }
- else
- {
- mainContentViewModel.CreateFile(path);
- }
- IsLoading = Visibility.Collapsed;
- }
- });
- }
- /// <summary>
- /// 打开文件
- /// </summary>
- public async void OpenFile()
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- openFileDialog.Filter = Properties.Resources.OpenDialogFilter;
- openFileDialog.Multiselect = true;
- if ((bool)openFileDialog.ShowDialog())
- {
- IsLoading = Visibility.Visible;
- 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]);
- //这一段很重要 如果注释掉将不能多选文件打开
- await Task.Delay(30);
- }
- ToolMethod.SetFileThumbImg(fileList[i]);
- }
- }
- IsLoading = Visibility.Collapsed;
- }
- }
- /// <summary>
- /// 创建空白文档
- /// </summary>
- 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
- }
- }
|