139 lines
5.4 KiB
C#
139 lines
5.4 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// PDF 阅读器窗口 ViewModel | PDF viewer window ViewModel
|
|
/// 轻量级 ViewModel,核心功能由 RadPdfViewer 控件内置处理 | Lightweight ViewModel, core features handled by RadPdfViewer
|
|
/// </summary>
|
|
public class PdfViewerWindowViewModel : BindableBase
|
|
{
|
|
private readonly IPdfPrintService _printService;
|
|
private readonly ILoggerService _logger;
|
|
|
|
/// <summary>
|
|
/// 窗口标题(含文件名)| Window title (with file name)
|
|
/// </summary>
|
|
public string Title { get; }
|
|
|
|
/// <summary>
|
|
/// PDF 文件路径(用于打印)| PDF file path (for printing)
|
|
/// </summary>
|
|
public string? FilePath { get; }
|
|
|
|
/// <summary>
|
|
/// PDF 文件流(用于流加载场景)| PDF file stream (for stream loading scenario)
|
|
/// </summary>
|
|
public Stream? PdfStream { get; }
|
|
|
|
/// <summary>
|
|
/// 打印命令(打开打印设置对话框)| Print command (open print settings dialog)
|
|
/// </summary>
|
|
public DelegateCommand PrintCommand { get; }
|
|
|
|
/// <summary>
|
|
/// 打印预览命令 | Print preview command
|
|
/// </summary>
|
|
public DelegateCommand PrintPreviewCommand { get; }
|
|
|
|
/// <summary>
|
|
/// 通过文件路径创建 ViewModel | Create ViewModel by file path
|
|
/// </summary>
|
|
/// <param name="filePath">PDF 文件路径 | PDF file path</param>
|
|
/// <param name="printService">打印服务 | Print service</param>
|
|
/// <param name="logger">日志服务 | Logger service</param>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 通过文件流创建 ViewModel | Create ViewModel by stream
|
|
/// </summary>
|
|
/// <param name="stream">PDF 文件流 | PDF file stream</param>
|
|
/// <param name="title">窗口标题(可选)| Window title (optional)</param>
|
|
/// <param name="printService">打印服务 | Print service</param>
|
|
/// <param name="logger">日志服务 | Logger service</param>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行打印(打开打印设置对话框)| Execute print (open print settings dialog)
|
|
/// </summary>
|
|
private void ExecutePrint()
|
|
{
|
|
try
|
|
{
|
|
_printService.PrintWithDialog(FilePath!);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Error(ex, "打印失败 | Print failed: {FilePath}", FilePath ?? string.Empty);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行打印预览 | Execute print preview
|
|
/// </summary>
|
|
private void ExecutePrintPreview()
|
|
{
|
|
try
|
|
{
|
|
_printService.PrintPreview(FilePath!);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Error(ex, "打印预览失败 | Print preview failed: {FilePath}", FilePath ?? string.Empty);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断打印命令是否可用(仅当 FilePath 有效时)| Check if print command is available (only when FilePath is valid)
|
|
/// </summary>
|
|
private bool CanExecutePrint()
|
|
{
|
|
return !string.IsNullOrEmpty(FilePath);
|
|
}
|
|
}
|
|
}
|