using Microsoft.Win32; using Prism.Commands; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ComPDFKitViewer.PdfViewer; using Prism.Regions; using DryIoc; using System.Diagnostics; using Prism.Services.Dialogs; using PDF_Office.CustomControl; using PDF_Office.Model; using PDF_Office.Tools; namespace PDF_Office.ViewModels { public class ViewContentViewModel : BindableBase, INavigationAware { public DelegateCommand LoadFile { get; set; } public DelegateCommand Load { get; set; } private CPDFViewer PDFViewer { get; set; } private MainContentViewModel mainViewModel { get; set; } public IRegionManager region; public IDialogService dialogs; public string ViwerRegionName { get; set; } public string BOTARegionName { get; set; } public string PropertyRegionName { get; set; } private bool isOpenFile = false; /// /// 鼠标滚轮缩放的缩放值 /// private double[] zoomLevel = { 1.00f, 10, 25, 50, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 }; public ViewContentViewModel(IRegionManager regionManager,IDialogService dialogService) { region = regionManager; dialogs = dialogService; LoadFile = new DelegateCommand(loadFile); Load = new DelegateCommand(LoadControl); ViwerRegionName = Guid.NewGuid().ToString(); BOTARegionName = Guid.NewGuid().ToString(); PropertyRegionName = Guid.NewGuid().ToString(); } private void LoadControl() { //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域 System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() => { region.RequestNavigate(BOTARegionName, "BOTAContent"); } )); } /// /// 将PDFViwer /// private void loadFile() { PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler; region.AddToRegion(ViwerRegionName, PDFViewer); } #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 public void OnNavigatedTo(NavigationContext navigationContext) { if (isOpenFile) return; var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel; if (mainVM != null) { mainViewModel = mainVM; } var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer; if (pdfview!=null) { PDFViewer = pdfview; loadFile(); } isOpenFile = true; } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } } }