将Feature/XP.Common和Feature/XP.Hardware分支合并至Develop/XP.forHardwareAndCommon,完善XPapp注册和相关硬件类库通用类库功能。
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using Prism.Mvvm;
|
||||
using XP.Common.Localization;
|
||||
|
||||
namespace XP.Common.GeneralForm.ViewModels
|
||||
{
|
||||
/// <summary>
|
||||
/// 输入对话框 ViewModel | Input dialog ViewModel
|
||||
/// </summary>
|
||||
public class InputDialogViewModel : BindableBase
|
||||
{
|
||||
private readonly Func<string, string?>? _validate;
|
||||
private string _inputText;
|
||||
private string? _validationError;
|
||||
|
||||
/// <summary>
|
||||
/// 窗口标题 | Window title
|
||||
/// </summary>
|
||||
public string Title { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 提示文本 | Prompt text
|
||||
/// </summary>
|
||||
public string Prompt { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 确定按钮文本(多语言)| OK button text (localized)
|
||||
/// </summary>
|
||||
public string OkText { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 取消按钮文本(多语言)| Cancel button text (localized)
|
||||
/// </summary>
|
||||
public string CancelText { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 用户输入的文本 | User input text
|
||||
/// </summary>
|
||||
public string InputText
|
||||
{
|
||||
get => _inputText;
|
||||
set
|
||||
{
|
||||
if (SetProperty(ref _inputText, value))
|
||||
{
|
||||
// 输入变化时清除验证错误 | Clear validation error on input change
|
||||
ValidationError = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证错误信息,null 表示无错误 | Validation error message, null means no error
|
||||
/// </summary>
|
||||
public string? ValidationError
|
||||
{
|
||||
get => _validationError;
|
||||
set
|
||||
{
|
||||
if (SetProperty(ref _validationError, value))
|
||||
{
|
||||
RaisePropertyChanged(nameof(HasValidationError));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否存在验证错误 | Whether there is a validation error
|
||||
/// </summary>
|
||||
public bool HasValidationError => !string.IsNullOrEmpty(ValidationError);
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数 | Constructor
|
||||
/// </summary>
|
||||
/// <param name="prompt">提示文本 | Prompt text</param>
|
||||
/// <param name="title">窗口标题 | Window title</param>
|
||||
/// <param name="defaultValue">默认值 | Default value</param>
|
||||
/// <param name="validate">验证委托,返回 null 表示通过,返回错误信息则显示 | Validation delegate</param>
|
||||
public InputDialogViewModel(
|
||||
string prompt,
|
||||
string title,
|
||||
string defaultValue = "",
|
||||
Func<string, string?>? validate = null)
|
||||
{
|
||||
Prompt = prompt;
|
||||
Title = title;
|
||||
_inputText = defaultValue;
|
||||
_validate = validate;
|
||||
|
||||
// 按钮文本使用多语言 | Button text uses localization
|
||||
OkText = LocalizationHelper.Get("Button_OK");
|
||||
CancelText = LocalizationHelper.Get("Button_Cancel");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行验证,返回是否通过 | Run validation, returns whether it passed
|
||||
/// </summary>
|
||||
/// <returns>true 表示验证通过 | true means validation passed</returns>
|
||||
public bool Validate()
|
||||
{
|
||||
if (_validate == null)
|
||||
return true;
|
||||
|
||||
var error = _validate(InputText);
|
||||
ValidationError = error;
|
||||
return error == null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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