using ComPDFKitViewer; using ComPDFKitViewer.PdfViewer; using Microsoft.Office.Interop.Word; using PDF_Office.CustomControl; using PDF_Office.Helper; using PDF_Office.Model; using PDF_Office.ViewModels.BOTA; using PDF_Office.Views; using PDFSettings.Settings; 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.Forms; namespace PDF_Office.ViewModels.PropertyPanel.ViewModular { public class ViewModularContentViewModel : BindableBase, INavigationAware { private IRegionManager region; private IDialogService dialogs; //记录 单页 双页 书本模式 private ViewMode viewmode = ViewMode.Single; public CPDFViewer PDFViewer { get; set; } public CPDFViewer SplitScreenPDFViewer { get; set; } public BottomToolContentViewModel BottomToolContentViewModel { get; set; } public ViewContentViewModel ViewContentViewModel { get; set; } public string SplitScreenViewRegionName { get; set; } public string ThemesContentName { get; set; } private Visibility splitScreenViewVisible = Visibility.Collapsed; /// /// 控制Content的显示 用于显示分屏的模块 /// public Visibility SplitScreenViewVisible { get { return splitScreenViewVisible; } set { SetProperty(ref splitScreenViewVisible, value); } } private bool isContinue; public bool IsContinue { get { return isContinue; } set { SetProperty(ref isContinue, value); if (value) { SetModeView(); } } } private bool isPagesBreak = true; public bool IsPagesBreak { get { return isPagesBreak; } set { SetProperty(ref isPagesBreak, value); } } private bool isSingleView; public bool IsSingleView { get { return isSingleView; } set { SetProperty(ref isSingleView, value); if (value) { SetModeView(); } } } private bool isTwoPageView; public bool IsTwoPageView { get { return isTwoPageView; } set { SetProperty(ref isTwoPageView, value); if (value) { SetModeView(); } } } private bool isBookModeView; public bool IsBookModeView { get { return isBookModeView; } set { SetProperty(ref isBookModeView, value); if (value) { SetModeView(); } } } private bool rBtnFullScreenIsChecked = false; public bool IsFullScreen { get { return rBtnFullScreenIsChecked; } set { SetProperty(ref rBtnFullScreenIsChecked, value); } } public DelegateCommand SplitScreenCommand { get; set; } public DelegateCommand DisableCommand { get; set; } public DelegateCommand SetViewModeCommand { get; set; } public DelegateCommand ContinueCommand { get; set; } public DelegateCommand PagesBreakCommand { get; set; } public DelegateCommand RotateCommand { get; set; } public DelegateCommand OpenFullCommand { get; set; } public ViewModularContentViewModel(IRegionManager regionManager, IDialogService dialogService) { region = regionManager; dialogs = dialogService; //未显示时无法注册上Region名称,所以需要短暂显示 //SplitScreenViewVisible = Visibility.Visible; //SplitScreenViewRegionName = RegionNames.SplitScreenViewRegionName; ThemesContentName = RegionNames.ThemesContentName; //SplitScreenViewVisible = Visibility.Collapsed; SplitScreenCommand = new DelegateCommand(SplitScreenEvent); DisableCommand = new DelegateCommand(DisableEvent); SetViewModeCommand = new DelegateCommand(SetModeView); ContinueCommand = new DelegateCommand(ContinueEvent); PagesBreakCommand = new DelegateCommand(PagesBreakEvent); RotateCommand = new DelegateCommand(RotateEvent); OpenFullCommand = new DelegateCommand(OpenFullWindow); //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域 System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() => { NavigationParameters parameters = new NavigationParameters(); parameters.Add(ParameterNames.PDFViewer, PDFViewer); parameters.Add(ParameterNames.ViewModularContentViewModel, this); region.RequestNavigate(RegionNames.ThemesContentName, "ThemesContent", parameters); } )); } /// /// 进入全屏模式 /// private void OpenFullWindow() { DialogParameters parameters = new DialogParameters(); //因为全屏模式可能需要设置特定的页面模式,所以只传文件路径,新建一个PDFview对象 parameters.Add(ParameterNames.FilePath, PDFViewer.Document.FilePath); parameters.Add(ParameterNames.PassWord, PDFViewer.Tag == null ? "" : PDFViewer.Tag.ToString()); dialogs.ShowDialog(DialogNames.FullScreenDialog, parameters, e => { if (e.Result == ButtonResult.Cancel) { //TODO:弹窗提示打开全屏模式失败 AlertsMessage alertsMessage = new AlertsMessage(); alertsMessage.ShowDialog("", "Error", "OK"); return; } else if (e.Result == ButtonResult.OK) { IsFullScreen = false; IsSingleView = true; } }); } /// /// 旋转 /// /// private void RotateEvent(object obj) { bool right = Convert.ToBoolean(obj); PDFViewer.RotatePage(PageRotate.Rotate90, right, PDFViewer.CurrentIndex); if (SplitScreenPDFViewer != null) { SplitScreenPDFViewer.RotatePage(PageRotate.Rotate90, right, SplitScreenPDFViewer.CurrentIndex); } //后续需要添加,缩略图旋转改变 PDFViewer.UndoManager.CanSave = true; } /// /// 分页符 /// /// private void PagesBreakEvent(object obj) { if (IsPagesBreak) { PDFViewer.SetPageSpacing(8); IsPagesBreak = true; } else { PDFViewer.SetPageSpacing(0); IsPagesBreak = false; } SaveMode(IsPagesBreak); } /// /// 设置显示模式 /// private void SetModeView() { if (PDFViewer != null) { SetUpModeView(PDFViewer); } //if (App.SplitScreenPDFViewer != null) //{ // SetUpModeView(App.SplitScreenPDFViewer); // SaveMode(App.SplitScreenPDFViewer.ModeView); //} SaveMode(PDFViewer.ModeView); } private void SetUpModeView(CPDFViewer pDFViewer) { if (IsContinue) { if (IsSingleView) { pDFViewer.ChangeViewMode(ViewMode.SingleContinuous); //App.SplitScreenViewMode = ViewMode.SingleContinuous; } else if (IsTwoPageView) { pDFViewer.ChangeViewMode(ViewMode.DoubleContinuous); //App.SplitScreenViewMode = ViewMode.DoubleContinuous; } else { pDFViewer.ChangeViewMode(ViewMode.BookContinuous); //App.SplitScreenViewMode = ViewMode.BookContinuous; } } else { if (IsSingleView) { pDFViewer.ChangeViewMode(ViewMode.Single); //App.SplitScreenViewMode = ViewMode.Single; } else if (IsTwoPageView) { pDFViewer.ChangeViewMode(ViewMode.Double); //App.SplitScreenViewMode = ViewMode.Double; } else { pDFViewer.ChangeViewMode(ViewMode.Book); //App.SplitScreenViewMode = ViewMode.Book; } } } /// /// 连续滚动 /// /// private void ContinueEvent(object obj) { SetModeView(); } /// /// 保存模式到本地 /// /// private void SaveMode(object modeView) { OpenFileInfo fileInfo = SettingHelper.GetFileInfo(PDFViewer.Document.FilePath); if (fileInfo != null) { if (modeView is SplitMode) { fileInfo.LastSplitMode = PDFViewer.Mode; } else if (modeView is ViewMode) { fileInfo.LastViewMode = PDFViewer.ModeView; } else if (modeView is bool) { fileInfo.LastPageSpace = (bool)modeView; } SettingHelper.SetFileInfo(fileInfo); } } /// /// 分屏视图-单屏 /// /// private void DisableEvent(object obj) { ViewContentViewModel.EnterSplitMode(EventAggregators.SplitMode.Single); } /// /// 分屏视图-垂直 /// /// private void SplitScreenEvent(object obj) { if (obj is System.Windows.Controls.RadioButton radioButton) { IsContinue = true; IsPagesBreak = true; switch (radioButton.Tag.ToString()) { case "VerticalSplit": ViewContentViewModel.EnterSplitMode(EventAggregators.SplitMode.Vertical); break; case "HorizontalSplit": ViewContentViewModel.EnterSplitMode(EventAggregators.SplitMode.Horizontal); break; } } } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } public void OnNavigatedTo(NavigationContext navigationContext) { CPDFViewer pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer; BottomToolContentViewModel btnTool = navigationContext.Parameters["BottomToolContentViewModel"] as BottomToolContentViewModel; if (pdfview != null && btnTool != null) { BottomToolContentViewModel = btnTool; PDFViewer = pdfview; ViewContentViewModel = BottomToolContentViewModel.ViewContentViewModel; } else { return; } IsSingleView = BottomToolContentViewModel.IsSingleView; IsTwoPageView = BottomToolContentViewModel.IsDoubleView; IsBookModeView = BottomToolContentViewModel.IsBookMode; IsContinue = BottomToolContentViewModel.IsContinue; PDFViewer.InfoChanged += PDFViewer_InfoChanged; } /// /// 和底部工具栏联动 /// /// /// private void PDFViewer_InfoChanged(object sender, KeyValuePair e) { if (e.Key == "ViewMode") { GetModeView((ViewMode)e.Value); } } private void GetModeView(ViewMode mode) { if ((int)mode % 2 == 0) { if (!IsContinue) { IsContinue = true; } } else { if (IsContinue) { IsContinue = false; } } if ((int)mode <= 2) { if (!isSingleView) { IsSingleView = true; } } else if ((int)mode <= 4) { if (!isTwoPageView) { IsTwoPageView = true; } } else { if (!isBookModeView) { IsBookModeView = true; } } } } }