将Feature/XP.Common和Feature/XP.Hardware分支合并至Develop/XP.forHardwareAndCommon,完善XPapp注册和相关硬件类库通用类库功能。
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using Prism.Mvvm;
|
||||
using XP.Common.Logging.Interfaces;
|
||||
|
||||
namespace XP.Common.GeneralForm.ViewModels
|
||||
{
|
||||
/// <summary>
|
||||
/// 进度条窗口 ViewModel,管理进度数据和窗口行为逻辑
|
||||
/// </summary>
|
||||
public class ProgressWindowViewModel : BindableBase
|
||||
{
|
||||
private readonly ILoggerService? _logger;
|
||||
private string _message;
|
||||
private double _progress;
|
||||
|
||||
/// <summary>
|
||||
/// 窗口标题(只读)
|
||||
/// </summary>
|
||||
public string Title { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 提示信息文本
|
||||
/// </summary>
|
||||
public string Message
|
||||
{
|
||||
get => _message;
|
||||
set => SetProperty(ref _message, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 进度值,范围 0-100
|
||||
/// </summary>
|
||||
public double Progress
|
||||
{
|
||||
get => _progress;
|
||||
set
|
||||
{
|
||||
if (SetProperty(ref _progress, value))
|
||||
{
|
||||
RaisePropertyChanged(nameof(ProgressText));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 百分比显示文本,由 Progress 自动计算(只读)
|
||||
/// </summary>
|
||||
public string ProgressText => $"{Math.Floor(Progress)}%";
|
||||
|
||||
/// <summary>
|
||||
/// 是否允许用户关闭窗口(只读)
|
||||
/// </summary>
|
||||
public bool IsCancelable { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="title">窗口标题</param>
|
||||
/// <param name="message">提示信息</param>
|
||||
/// <param name="progress">初始进度值</param>
|
||||
/// <param name="isCancelable">是否允许取消</param>
|
||||
/// <param name="logger">日志服务(可选)</param>
|
||||
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<ProgressWindowViewModel>();
|
||||
|
||||
// 构造时记录 Info 级别日志
|
||||
_logger?.Info("进度窗口已创建:Title={Title}, IsCancelable={IsCancelable}", Title, IsCancelable);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新进度和提示信息(唯一的外部更新入口)
|
||||
/// </summary>
|
||||
/// <param name="message">提示信息文本</param>
|
||||
/// <param name="progress">进度值,超出 [0, 100] 范围时自动修正</param>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user