Files
DMS/DMS.WPF/ViewModels/SettingViewModel.cs
David P.G ec1f94a898 1 refactor(config): 将AppSettings移至Application层并更新配置结构
2
    3 - 将AppSettings从DMS.Infrastructure移至DMS.Application
    4 - 将DatabaseSettings重命名为AppSettings.Database并更新所有引用
    5 - 将配置文件从appSettings.json更改为dms_config.json
    6 - 更新所有项目引用以使用新的AppSettings命名空间
    7 - 移除DI容器中的SqlSugarDbContext直接实例化
    8 - 添加Material Design Icons字体并更新设置视图UI
    9 - 通过移除不必要的变量映射更新来优化S7服务
   10 - 将数据库连接字符串属性名从Database更新为DbName
2025-10-04 18:42:12 +08:00

174 lines
4.3 KiB
C#

using CommunityToolkit.Mvvm.Input;
using DMS.Application.Configurations;
using DMS.WPF.Helper;
using DMS.WPF.Interfaces;
namespace DMS.WPF.ViewModels;
public partial class SettingViewModel : ViewModelBase
{
private AppSettings _settings;
private readonly INotificationService _notificationService;
public SettingViewModel(AppSettings appSettings,INotificationService notificationService)
{
_settings = appSettings;
_notificationService = notificationService;
AvailableDbTypes = Enum.GetNames(typeof(SqlSugar.DbType)).ToList();
Themes = new List<string> { "浅色", "深色", "跟随系统" };
}
public List<string> Themes { get; }
public string SelectedTheme
{
get => _settings.Theme;
set
{
if (_settings.Theme != value)
{
_settings.Theme = value;
OnPropertyChanged();
_settings.Save();
ThemeHelper.ApplyTheme(value);
}
}
}
public List<string> AvailableDbTypes { get; set; }
public string SelectedDbType
{
get => _settings.Db.DbType;
set
{
if (_settings.Db.DbType != value)
{
_settings.Db.DbType = value;
OnPropertyChanged();
_settings.Save();
}
}
}
public string Server
{
get => _settings.Db.Server;
set
{
if (_settings.Db.Server != value)
{
_settings.Db.Server = value;
OnPropertyChanged();
_settings.Save();
}
}
}
public int Port
{
get => _settings.Db.Port;
set
{
if (_settings.Db.Port != value)
{
_settings.Db.Port = value;
OnPropertyChanged();
_settings.Save();
}
}
}
public string UserId
{
get => _settings.Db.UserId;
set
{
if (_settings.Db.UserId != value)
{
_settings.Db.UserId = value;
OnPropertyChanged();
_settings.Save();
}
}
}
public string Password
{
get => _settings.Db.Password;
set
{
if (_settings.Db.Password != value)
{
_settings.Db.Password = value;
OnPropertyChanged();
_settings.Save();
}
}
}
public string DbName
{
get => _settings.Db.DbName;
set
{
if (_settings.Db.DbName != value)
{
_settings.Db.DbName = value;
OnPropertyChanged();
_settings.Save();
}
}
}
public bool MinimizeToTrayOnClose
{
get => _settings.MinimizeToTrayOnClose;
set
{
if (_settings.MinimizeToTrayOnClose != value)
{
_settings.MinimizeToTrayOnClose = value;
OnPropertyChanged(nameof(MinimizeToTrayOnClose));
_settings.Save();
}
}
}
public int DefaultPollingInterval
{
get => _settings.DefaultPollingInterval;
set
{
if (_settings.DefaultPollingInterval != value)
{
_settings.DefaultPollingInterval = value;
OnPropertyChanged();
_settings.Save();
}
}
}
[RelayCommand]
private async Task TestConnection()
{
try
{
// 使用当前配置测试数据库连接
using var db = new SqlSugar.SqlSugarScope(new SqlSugar.ConnectionConfig()
{
DbType = (SqlSugar.DbType)Enum.Parse(typeof(SqlSugar.DbType), _settings.Db.DbType),
ConnectionString = _settings.ToConnectionString(),
IsAutoCloseConnection = true
});
await db.Ado.ExecuteCommandAsync("SELECT 1");
_notificationService.ShowSuccess("连接成功!");
}
catch (Exception ex)
{
_notificationService.ShowError($"连接失败:{ex.Message}", ex);
}
}
}