123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- 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 System.Windows;
- using System.Windows.Controls;
- namespace PDF_Office.ViewModels
- {
- public class ViewContentViewModel : BindableBase, INavigationAware
- {
- #region 属性、变量
- 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; }
- public string ToolContentRegionName { get; set; }
- private int gridToolRow = 1;
- /// <summary>
- /// 控制ToolContent的Row
- /// </summary>
- public int GridToolRow
- {
- get { return gridToolRow; }
- set
- {
- SetProperty(ref gridToolRow, value);
- }
- }
- private int gridToolRowSpan = 3;
- /// <summary>
- /// 控制ToolContent的RowSpan
- /// </summary>
- public int GridToolRowSpan
- {
- get { return gridToolRowSpan; }
- set
- {
- SetProperty(ref gridToolRowSpan, value);
- }
- }
- private Visibility toolContentVisible = Visibility.Visible;
- /// <summary>
- /// 控制Content的显示 用于显示水印、贝茨码、密文等功能模块
- /// 留意:显示前需要先注入内容、设置好行和跨行数
- /// </summary>
- public Visibility ToolContentVisible
- {
- get { return toolContentVisible; }
- set
- {
- SetProperty(ref toolContentVisible, value);
- }
- }
- private Visibility isLoading = Visibility.Collapsed;
- /// <summary>
- /// 是否正在加载中
- /// </summary>
- public Visibility IsLoading
- {
- get { return isLoading; }
- set
- {
- SetProperty(ref isLoading, value);
- }
- }
- /// <summary>
- /// 用来避免重复触发导航事件的标志符
- /// </summary>
- private bool isOpenFile = false;
- /// <summary>
- /// 鼠标滚轮缩放的缩放值
- /// </summary>
- private double[] zoomLevel = { 1.00f, 10, 25, 50, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
- #endregion
- #region 命令
- public DelegateCommand LoadFile { get; set; }
- public DelegateCommand Load { get; set; }
- public DelegateCommand<object> TabControlSelectionChangedCommand { get; set; }
- #endregion
- public ViewContentViewModel(IRegionManager regionManager, IDialogService dialogService)
- {
- region = regionManager;
- dialogs = dialogService;
- LoadFile = new DelegateCommand(loadFile);
- Load = new DelegateCommand(LoadControl);
- TabControlSelectionChangedCommand = new DelegateCommand<object>(TabControlSelectonChangedEvent);
- ViwerRegionName = Guid.NewGuid().ToString();
- BOTARegionName = Guid.NewGuid().ToString();
- PropertyRegionName = Guid.NewGuid().ToString();
- //未显示时无法注册上Region名称
- ToolContentVisible = Visibility.Visible;
- ToolContentRegionName = Guid.NewGuid().ToString();
- ToolContentVisible = Visibility.Collapsed;
- }
- private void LoadControl()
- {
- //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域
- System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
- {
- region.RequestNavigate(BOTARegionName, "BOTAContent");
- }
- ));
- }
- /// <summary>
- /// 将PDFViwer添加到Region
- /// </summary>
- private void loadFile()
- {
- PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
- region.AddToRegion(ViwerRegionName, PDFViewer);
- }
- /// <summary>
- /// 选项卡切换事件
- /// </summary>
- /// <param name="e"></param>
- private void TabControlSelectonChangedEvent(object e)
- {
- var args = e as SelectionChangedEventArgs;
- if (args != null)
- {
- var item = args.AddedItems[0] as TabItem;
- if (item.Name == "TabItemPageEdit")
- {
- EnterToolMode("PageEditContent");
- }
- else
- {
- ExitToolMode();
- }
- }
- }
- #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
- #region Navigate
- 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)
- {
- }
- #endregion
- #region 方法
- /// <summary>
- /// 显示前添加内容到Region
- /// </summary>
- /// <param name="isPageEdit"></param>
- private void ShowToolContent(bool isPageEdit = true)
- {
- //显示页面编辑
- if (isPageEdit)
- {
- if (GridToolRow != 1)
- {
- GridToolRow = 1;
- }
- if (GridToolRowSpan != 3)
- {
- GridToolRowSpan = 3;
- }
- }
- else
- {
- //显示其他工具模式 如密文、水印等
- GridToolRow = 0;
- GridToolRowSpan = 4;
- }
- ToolContentVisible = Visibility.Visible;
- }
- /// <summary>
- /// 进入工具编辑(如页面编辑、水印、密文等)模式
- /// </summary>
- /// <param name="targetToolMode">要导航过去的控件名称</param>
- /// <param name="valuePairs">导航需要传送的参数,为空时,默认传送PDFView和ViewContentViewModel</param>
- private async void EnterToolMode(string targetToolMode, NavigationParameters valuePairs = null)
- {
- IsLoading = Visibility.Visible;
- await Task.Delay(3);
- NavigationParameters param = new NavigationParameters();
- if (valuePairs == null)
- {
- param.Add(ParameterNames.PDFViewer, PDFViewer);
- param.Add(ParameterNames.ViewContentViewModel, this);
- }
- else//有传入其他内容的参数时
- {
- param = valuePairs;
- }
- region.RequestNavigate(ToolContentRegionName, targetToolMode, param);
- if (targetToolMode == "PageEditContent")
- {
- ShowToolContent();
- }
- else
- {
- ShowToolContent(false);
- }
- IsLoading = Visibility.Collapsed;
- }
- /// <summary>
- /// 退出工具(水印、密文等)编辑模式,隐藏ToolContent
- /// </summary>
- public void ExitToolMode()
- {
- ToolContentVisible = Visibility.Collapsed;
- }
- #endregion
- }
- }
|