using System; using Prism.Mvvm; using XP.Common.Logging.Interfaces; namespace XP.Common.GeneralForm.ViewModels { /// /// 进度条窗口 ViewModel,管理进度数据和窗口行为逻辑 /// public class ProgressWindowViewModel : BindableBase { private readonly ILoggerService? _logger; private string _message; private double _progress; /// /// 窗口标题(只读) /// public string Title { get; } /// /// 提示信息文本 /// public string Message { get => _message; set => SetProperty(ref _message, value); } /// /// 进度值,范围 0-100 /// public double Progress { get => _progress; set { if (SetProperty(ref _progress, value)) { RaisePropertyChanged(nameof(ProgressText)); } } } /// /// 百分比显示文本,由 Progress 自动计算(只读) /// public string ProgressText => $"{Math.Floor(Progress)}%"; /// /// 是否允许用户关闭窗口(只读) /// public bool IsCancelable { get; } /// /// 构造函数 /// /// 窗口标题 /// 提示信息 /// 初始进度值 /// 是否允许取消 /// 日志服务(可选) public ProgressWindowViewModel( string title = "操作进行中", string message = "请稍候...", double progress = 0, bool isCancelable = true, ILoggerService? logger = null) { Title = title; _message = message; _progress = progress; IsCancelable = isCancelable; // 创建模块化日志实例(logger 为 null 时静默跳过) _logger = logger?.ForModule(); // 构造时记录 Info 级别日志 _logger?.Info("进度窗口已创建:Title={Title}, IsCancelable={IsCancelable}", Title, IsCancelable); } /// /// 更新进度和提示信息(唯一的外部更新入口) /// /// 提示信息文本 /// 进度值,超出 [0, 100] 范围时自动修正 public void UpdateProgress(string message, double progress) { // 检查 progress 是否超出有效范围,记录 Warn 日志 if (progress < 0 || progress > 100) { _logger?.Warn("进度值超出有效范围 [0, 100]:{Progress},将自动修正", progress); } // 将 progress 值 Clamp 到 [0, 100] 范围 progress = Math.Clamp(progress, 0, 100); // 更新属性,触发 PropertyChanged 通知 Message = message; Progress = progress; } } }