将Feature/XP.Common和Feature/XP.Hardware分支合并至Develop/XP.forHardwareAndCommon,完善XPapp注册和相关硬件类库通用类库功能。

This commit is contained in:
QI Mingxuan
2026-04-16 17:31:13 +08:00
parent 6ec4c3ddaa
commit 2bd6e566c3
581 changed files with 74600 additions and 222 deletions
@@ -0,0 +1,39 @@
using XP.Common.Localization.Enums;
namespace XP.Common.Localization.ViewModels
{
/// <summary>
/// 语言选项数据模型 | Language option data model
/// 用于在 UI 中显示可选语言 | Used to display available languages in UI
/// </summary>
public class LanguageOption
{
/// <summary>
/// 语言枚举值 | Language enum value
/// </summary>
public SupportedLanguage Language { get; }
/// <summary>
/// 显示名称 | Display name
/// </summary>
public string DisplayName { get; }
/// <summary>
/// 语言标志/图标 | Language flag/icon
/// </summary>
public string Flag { get; }
/// <summary>
/// 构造函数 | Constructor
/// </summary>
/// <param name="language">语言 | Language</param>
/// <param name="displayName">显示名称 | Display name</param>
/// <param name="flag">标志 | Flag</param>
public LanguageOption(SupportedLanguage language, string displayName, string flag = "")
{
Language = language;
DisplayName = displayName;
Flag = flag;
}
}
}
@@ -0,0 +1,209 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using Prism.Commands;
using Prism.Mvvm;
using XP.Common.Localization.Enums;
using XP.Common.Localization.Interfaces;
using XP.Common.Logging.Interfaces;
namespace XP.Common.Localization.ViewModels
{
/// <summary>
/// 语言切换器 ViewModel | Language switcher ViewModel
/// 提供语言选择和切换功能 | Provides language selection and switching functionality
/// </summary>
public class LanguageSwitcherViewModel : BindableBase
{
private readonly ILocalizationService _localizationService;
private readonly ILocalizationConfig _config;
private readonly ILoggerService _logger;
private LanguageOption? _selectedLanguage;
/// <summary>
/// 选中的语言选项 | Selected language option
/// </summary>
public LanguageOption? SelectedLanguage
{
get => _selectedLanguage;
set
{
if (SetProperty(ref _selectedLanguage, value))
{
ApplyCommand.RaiseCanExecuteChanged();
UpdatePreviewTexts();
}
}
}
/// <summary>
/// 可用语言列表 | Available languages list
/// </summary>
public IEnumerable<LanguageOption> AvailableLanguages { get; }
/// <summary>
/// 应用按钮命令 | Apply button command
/// </summary>
public DelegateCommand ApplyCommand { get; }
private string _previewRestartNotice = string.Empty;
/// <summary>
/// 预览重启提示文本 | Preview restart notice text
/// </summary>
public string PreviewRestartNotice
{
get => _previewRestartNotice;
set => SetProperty(ref _previewRestartNotice, value);
}
private string _previewApplyButtonText = string.Empty;
/// <summary>
/// 预览应用按钮文本 | Preview apply button text
/// </summary>
public string PreviewApplyButtonText
{
get => _previewApplyButtonText;
set => SetProperty(ref _previewApplyButtonText, value);
}
/// <summary>
/// 构造函数 | Constructor
/// </summary>
/// <param name="localizationService">本地化服务 | Localization service</param>
/// <param name="config">本地化配置 | Localization configuration</param>
/// <param name="logger">日志服务 | Logger service</param>
public LanguageSwitcherViewModel(
ILocalizationService localizationService,
ILocalizationConfig config,
ILoggerService logger)
{
_localizationService = localizationService ?? throw new ArgumentNullException(nameof(localizationService));
_config = config ?? throw new ArgumentNullException(nameof(config));
_logger = logger.ForModule<LanguageSwitcherViewModel>();
// 初始化可用语言列表 | Initialize available languages list
AvailableLanguages = new[]
{
new LanguageOption(SupportedLanguage.ZhCN, "简体中文", "🇨🇳"),
new LanguageOption(SupportedLanguage.ZhTW, "繁體中文", "🇹🇼"),
new LanguageOption(SupportedLanguage.EnUS, "English", "🇺🇸")
};
// 设置当前选中的语言 | Set currently selected language
var currentLang = localizationService.CurrentLanguage;
foreach (var lang in AvailableLanguages)
{
if (lang.Language == currentLang)
{
_selectedLanguage = lang;
break;
}
}
// 初始化命令 | Initialize commands
ApplyCommand = new DelegateCommand(OnApply, CanApply);
// 初始化预览文本 | Initialize preview texts
UpdatePreviewTexts();
}
/// <summary>
/// 更新预览文本 | Update preview texts
/// </summary>
private void UpdatePreviewTexts()
{
if (_selectedLanguage == null)
return;
// 获取选中语言的 CultureInfo | Get CultureInfo for selected language
var previewCulture = GetCultureInfo(_selectedLanguage.Language);
// 使用选中的语言获取本地化文本 | Get localized text using selected language
PreviewRestartNotice = _localizationService.GetString("Settings_Language_RestartNotice", previewCulture);
PreviewApplyButtonText = _localizationService.GetString("Button_Apply", previewCulture);
}
/// <summary>
/// 判断是否可以应用 | Determine if can apply
/// 与配置文件中已保存的语言比较 | Compare with saved language in config
/// </summary>
private bool CanApply()
{
if (_selectedLanguage == null)
return false;
// 获取配置文件中已保存的语言 | Get saved language from config
var savedLanguage = _config.GetSavedLanguage() ?? _localizationService.CurrentLanguage;
return _selectedLanguage.Language != savedLanguage;
}
/// <summary>
/// 应用语言切换 | Apply language change
/// </summary>
private void OnApply()
{
if (_selectedLanguage == null)
return;
try
{
var newLanguage = _selectedLanguage.Language;
_localizationService.SetLanguage(newLanguage);
_logger.Info($"Language switched to {newLanguage}");
// 显示重启提示对话框 | Show restart prompt dialog
MessageBox.Show(
_localizationService.GetString("Settings_Language_SavedRestartRequired"),
_localizationService.GetString("Dialog_Notice"),
MessageBoxButton.OK,
MessageBoxImage.Information);
// 更新命令状态 | Update command state
ApplyCommand.RaiseCanExecuteChanged();
}
catch (Exception ex)
{
_logger.Error(ex, $"Failed to switch language to {_selectedLanguage.Language}");
// 显示错误对话框 | Show error dialog
var errorMsg = string.Format(
_localizationService.GetString("Settings_Language_SwitchFailed"),
ex.Message);
MessageBox.Show(
errorMsg,
_localizationService.GetString("Dialog_Error"),
MessageBoxButton.OK,
MessageBoxImage.Error);
// 恢复到之前的语言 | Restore previous language
var currentLang = _localizationService.CurrentLanguage;
foreach (var lang in AvailableLanguages)
{
if (lang.Language == currentLang)
{
_selectedLanguage = lang;
RaisePropertyChanged(nameof(SelectedLanguage));
break;
}
}
}
}
/// <summary>
/// 获取语言对应的 CultureInfo | Get CultureInfo for language
/// </summary>
private CultureInfo GetCultureInfo(SupportedLanguage language)
{
return language switch
{
SupportedLanguage.ZhCN => new CultureInfo("zh-CN"),
SupportedLanguage.ZhTW => new CultureInfo("zh-TW"),
SupportedLanguage.EnUS => new CultureInfo("en-US"),
_ => new CultureInfo("zh-CN")
};
}
}
}