123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- using ComPDFKitViewer.PdfViewer;
- using PDF_Office.Model;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Regions;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- namespace PDF_Office.ViewModels.Dialog
- {
- public class FullScreenWindowViewModel : BindableBase, IDialogAware
- {
- public string Title => "";
- private Visibility showTip = Visibility.Collapsed;
- public Visibility ShowTip
- {
- get { return showTip; }
- set
- {
- SetProperty(ref showTip, value);
- }
- }
- private CPDFViewer PDFViewer;
- public DelegateCommand ExitCommand { get; set; }
- public DelegateCommand<object> LoadCommand { get; set; }
- public event Action<IDialogResult> RequestClose;
- public string RegionName { get; set; }
- /// <summary>
- /// 鼠标滚轮缩放的缩放值
- /// </summary>
- private double[] zoomLevel = { 1.00f, 10, 25, 50, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
- public FullScreenWindowViewModel()
- {
- ExitCommand = new DelegateCommand(Exit);
- LoadCommand = new DelegateCommand<object>(Load);
- RegionName = Guid.NewGuid().ToString();
- }
- private void Exit()
- {
- RequestClose.Invoke(new DialogResult(ButtonResult.OK));
- }
- private void Load(object grid)
- {
- var Grid = grid as Grid;
- if(Grid!=null&&PDFViewer!=null)
- {
- Grid.Children.Insert(0, PDFViewer);
- ShowTip = Visibility.Visible;
- }
- }
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- if(PDFViewer!=null&&PDFViewer.Document!=null)
- {
- PDFViewer.Document.Release();
- }
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- string filepath;
- string password;
- parameters.TryGetValue<string>(ParameterNames.FilePath,out filepath);
- parameters.TryGetValue<string>(ParameterNames.PassWord,out password);
- if (!string.IsNullOrEmpty(filepath))
- {
- PDFViewer = new CPDFViewer();
- PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
- PDFViewer.InitDocument(filepath);
- if (PDFViewer.Document == null)
- {
- RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
- }
- if (PDFViewer.Document.IsLocked && !string.IsNullOrEmpty(password))
- {
- PDFViewer.Document.UnlockWithPassword(password);
- if (PDFViewer.Document.IsLocked)
- {
- var dialogservice = Prism.Ioc.ContainerLocator.Container.Resolve(typeof(IDialogService)) as IDialogService;
- DialogParameters pairs = new DialogParameters();
- pairs.Add(ParameterNames.PDFDocument,PDFViewer.Document);
- dialogservice.ShowDialog(DialogNames.VerifyPassWordDialog,pairs,e=> {
- if(e.Result!=ButtonResult.OK)
- {
- RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
- }
- });
- }
- }
- PDFViewer.Load();
- PDFViewer.SetMouseMode(MouseModes.Default);
- }
- }
- #region PDFViewer缩放事件
- public void PdfViewer_MouseWheelZoomHandler(object sender, bool e)
- {
- double newZoom = CheckZoomLevel(PDFViewer.ZoomFactor + (e ? 0.01 : -0.01), e);
- PDFViewer.Zoom(newZoom);
- }
- private double CheckZoomLevel(double zoom, bool IsGrowth)
- {
- double standardZoom = 100;
- if (zoom <= 0.01)
- {
- return 0.01;
- }
- if (zoom >= 10)
- {
- return 10;
- }
- zoom *= 100;
- for (int i = 0; i < zoomLevel.Length - 1; i++)
- {
- if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth)
- {
- standardZoom = zoomLevel[i + 1];
- break;
- }
- if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth)
- {
- standardZoom = zoomLevel[i];
- break;
- }
- }
- return standardZoom / 100;
- }
- #endregion
- }
- }
|