using System; using System.IO; using Prism.Commands; using Prism.Mvvm; using XP.Common.Localization; using XP.Common.Logging.Interfaces; using XP.Common.PdfViewer.Interfaces; namespace XP.Common.PdfViewer.ViewModels { /// /// PDF 阅读器窗口 ViewModel | PDF viewer window ViewModel /// 轻量级 ViewModel,核心功能由 RadPdfViewer 控件内置处理 | Lightweight ViewModel, core features handled by RadPdfViewer /// public class PdfViewerWindowViewModel : BindableBase { private readonly IPdfPrintService _printService; private readonly ILoggerService _logger; /// /// 窗口标题(含文件名)| Window title (with file name) /// public string Title { get; } /// /// PDF 文件路径(用于打印)| PDF file path (for printing) /// public string? FilePath { get; } /// /// PDF 文件流(用于流加载场景)| PDF file stream (for stream loading scenario) /// public Stream? PdfStream { get; } /// /// 打印命令(打开打印设置对话框)| Print command (open print settings dialog) /// public DelegateCommand PrintCommand { get; } /// /// 打印预览命令 | Print preview command /// public DelegateCommand PrintPreviewCommand { get; } /// /// 通过文件路径创建 ViewModel | Create ViewModel by file path /// /// PDF 文件路径 | PDF file path /// 打印服务 | Print service /// 日志服务 | Logger service public PdfViewerWindowViewModel( string filePath, IPdfPrintService printService, ILoggerService logger) { _printService = printService ?? throw new ArgumentNullException(nameof(printService)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); FilePath = filePath ?? throw new ArgumentNullException(nameof(filePath)); PdfStream = null; // 使用多语言标题,包含文件名 | Use localized title with file name var fileName = Path.GetFileName(filePath); Title = LocalizationHelper.Get("PdfViewer_TitleWithFile", fileName); // 初始化命令 | Initialize commands PrintCommand = new DelegateCommand(ExecutePrint, CanExecutePrint); PrintPreviewCommand = new DelegateCommand(ExecutePrintPreview, CanExecutePrint); } /// /// 通过文件流创建 ViewModel | Create ViewModel by stream /// /// PDF 文件流 | PDF file stream /// 窗口标题(可选)| Window title (optional) /// 打印服务 | Print service /// 日志服务 | Logger service public PdfViewerWindowViewModel( Stream stream, string? title, IPdfPrintService printService, ILoggerService logger) { _printService = printService ?? throw new ArgumentNullException(nameof(printService)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); PdfStream = stream ?? throw new ArgumentNullException(nameof(stream)); FilePath = null; // 使用自定义标题或默认多语言标题 | Use custom title or default localized title Title = !string.IsNullOrWhiteSpace(title) ? title : LocalizationHelper.Get("PdfViewer_Title"); // 初始化命令(流模式下无文件路径,命令不可用)| Initialize commands (no file path in stream mode, commands disabled) PrintCommand = new DelegateCommand(ExecutePrint, CanExecutePrint); PrintPreviewCommand = new DelegateCommand(ExecutePrintPreview, CanExecutePrint); } /// /// 执行打印(打开打印设置对话框)| Execute print (open print settings dialog) /// private void ExecutePrint() { try { _printService.PrintWithDialog(FilePath!); } catch (Exception ex) { _logger.Error(ex, "打印失败 | Print failed: {FilePath}", FilePath ?? string.Empty); } } /// /// 执行打印预览 | Execute print preview /// private void ExecutePrintPreview() { try { _printService.PrintPreview(FilePath!); } catch (Exception ex) { _logger.Error(ex, "打印预览失败 | Print preview failed: {FilePath}", FilePath ?? string.Empty); } } /// /// 判断打印命令是否可用(仅当 FilePath 有效时)| Check if print command is available (only when FilePath is valid) /// private bool CanExecutePrint() { return !string.IsNullOrEmpty(FilePath); } } }