From 1a666b79c8828300708931bd0b026e8a3140d169 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Thu, 2 Oct 2025 12:35:42 +0800 Subject: [PATCH] =?UTF-8?q?=201.=20=E5=9C=A8=20AppSettings=20=E7=B1=BB?= =?UTF-8?q?=E4=B8=AD=E6=B7=BB=E5=8A=A0=E4=BA=86=20DefaultPollingInterval?= =?UTF-8?q?=20=E5=B1=9E=E6=80=A7=EF=BC=8C=E7=94=A8=E4=BA=8E=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E5=85=A8=E5=B1=80=E9=BB=98=E8=AE=A4=E8=BD=AE=E8=AF=A2?= =?UTF-8?q?=E9=97=B4=E9=9A=94=E5=80=BC=20=20=20=202.=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E4=BA=86=20SettingViewModel=20=E4=BB=A5=E5=8C=85=E5=90=AB?= =?UTF-8?q?=E5=AF=B9=E8=BD=AE=E8=AF=A2=E9=97=B4=E9=9A=94=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E7=9A=84=E6=94=AF=E6=8C=81=EF=BC=8C=E5=85=81=E8=AE=B8=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=9C=A8=E8=AE=BE=E7=BD=AE=E9=A1=B5=E9=9D=A2=E4=B8=AD?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AF=A5=E5=80=BC=20=20=20=203.=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=BA=86=20VariableItemViewModel=EF=BC=8C=E4=BD=BF?= =?UTF-8?q?=E5=85=B6=E5=9C=A8=E5=88=9D=E5=A7=8B=E5=8C=96=E6=97=B6=E4=BB=8E?= =?UTF-8?q?=20AppSettings=20=E5=8A=A0=E8=BD=BD=E9=BB=98=E8=AE=A4=E8=BD=AE?= =?UTF-8?q?=E8=AF=A2=E9=97=B4=E9=9A=94=E5=80=BC=20=20=20=204.=20=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E4=BA=86=20SettingView.xaml=EF=BC=8C=E5=9C=A8?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E9=A1=B5=E9=9D=A2=E7=9A=84"=E9=80=9A?= =?UTF-8?q?=E7=94=A8=E8=AE=BE=E7=BD=AE"=E9=83=A8=E5=88=86=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=BA=86=E8=BD=AE=E8=AF=A2=E9=97=B4=E9=9A=94=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Configurations/AppSettings.cs | 12 +- DMS.WPF/Profiles/MappingProfile.cs | 3 +- .../ViewModels/Items/VariableItemViewModel.cs | 12 +- DMS.WPF/ViewModels/SettingViewModel.cs | 272 ++++++++++-------- DMS.WPF/ViewModels/SplashViewModel.cs | 7 +- DMS.WPF/Views/SettingView.xaml | 5 + 6 files changed, 181 insertions(+), 130 deletions(-) diff --git a/DMS.Infrastructure/Configurations/AppSettings.cs b/DMS.Infrastructure/Configurations/AppSettings.cs index 9e9c045..c4554a9 100644 --- a/DMS.Infrastructure/Configurations/AppSettings.cs +++ b/DMS.Infrastructure/Configurations/AppSettings.cs @@ -1,3 +1,4 @@ +using AutoMapper; using DMS.Core.Models; using Newtonsoft.Json; @@ -15,6 +16,10 @@ namespace DMS.Infrastructure.Configurations public class AppSettings { + private readonly IMapper _mapper; + + + public DatabaseSettings Database { get; set; } = new DatabaseSettings(); public string Theme { get; set; } = "跟随系统"; public bool EnableS7Service { get; set; } = true; @@ -22,15 +27,18 @@ namespace DMS.Infrastructure.Configurations public bool EnableOpcUaService { get; set; } = true; public bool MinimizeToTrayOnClose { get; set; } = true; public List Menus { get; set; } = new List(); + public int DefaultPollingInterval { get; set; } = 30000; // 默认轮询间隔30秒 private static readonly string SettingsFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appSettings.json"); - public static AppSettings Load() + public AppSettings Load() { if (File.Exists(SettingsFilePath)) { string json = File.ReadAllText(SettingsFilePath); - return JsonConvert.DeserializeObject(json); + AppSettings appSettings = JsonConvert.DeserializeObject(json); + + return appSettings; } return new AppSettings(); } diff --git a/DMS.WPF/Profiles/MappingProfile.cs b/DMS.WPF/Profiles/MappingProfile.cs index 083a794..907c2f5 100644 --- a/DMS.WPF/Profiles/MappingProfile.cs +++ b/DMS.WPF/Profiles/MappingProfile.cs @@ -28,7 +28,8 @@ namespace DMS.WPF.Profiles CreateMap().ReverseMap(); CreateMap().ReverseMap(); CreateMap().ReverseMap(); - CreateMap().ReverseMap(); + CreateMap() + .ReverseMap(); CreateMap().ReverseMap(); CreateMap().ReverseMap(); CreateMap().ReverseMap(); diff --git a/DMS.WPF/ViewModels/Items/VariableItemViewModel.cs b/DMS.WPF/ViewModels/Items/VariableItemViewModel.cs index c0ebb53..36ad484 100644 --- a/DMS.WPF/ViewModels/Items/VariableItemViewModel.cs +++ b/DMS.WPF/ViewModels/Items/VariableItemViewModel.cs @@ -3,6 +3,8 @@ using DMS.Application.DTOs; using DMS.Core.Enums; using System; using System.Collections.Generic; +using DMS.Infrastructure.Configurations; +using Microsoft.Extensions.DependencyInjection; namespace DMS.WPF.ViewModels.Items; @@ -116,7 +118,15 @@ public partial class VariableItemViewModel : ObservableObject /// 用于决定数据采集的频率。 /// [ObservableProperty] - private int _pollingInterval = 30000; // ThirtySeconds + private int _pollingInterval; + + public VariableItemViewModel() + { + // 使用默认值,实际的默认值应该从外部传入或通过其他方式设置 + // 保持构造函数轻量级,不加载配置文件 + _pollingInterval = App.Current.Services.GetRequiredService().DefaultPollingInterval; // 默认值,可通过外部设置覆盖 + } + /// /// 获取或设置一个值,该值指示此变量是否被激活。 diff --git a/DMS.WPF/ViewModels/SettingViewModel.cs b/DMS.WPF/ViewModels/SettingViewModel.cs index 1cfda01..dd00205 100644 --- a/DMS.WPF/ViewModels/SettingViewModel.cs +++ b/DMS.WPF/ViewModels/SettingViewModel.cs @@ -1,151 +1,173 @@ using CommunityToolkit.Mvvm.Input; +using DMS.Infrastructure.Configurations; using DMS.WPF.Helper; +using DMS.WPF.Interfaces; namespace DMS.WPF.ViewModels; public partial class SettingViewModel : ViewModelBase { - // private AppSettings _settings; + private AppSettings _settings; + private readonly INotificationService _notificationService; - public SettingViewModel() + public SettingViewModel(AppSettings appSettings,INotificationService notificationService) { - // _settings = AppSettings.Load(); - // AvailableDbTypes = Enum.GetNames(typeof(SqlSugar.DbType)).ToList(); - // Themes = new List { "浅色", "深色", "跟随系统" }; - // this.transaction = transaction; + _settings = appSettings; + _notificationService = notificationService; + AvailableDbTypes = Enum.GetNames(typeof(SqlSugar.DbType)).ToList(); + Themes = new List { "浅色", "深色", "跟随系统" }; } public List Themes { get; } - // public string SelectedTheme - // { - // get => _settings.Theme; - // set - // { - // if (_settings.Theme != value) - // { - // _settings.Theme = value; - // OnPropertyChanged(); - // _settings.Save(); - // ThemeHelper.ApplyTheme(value); - // } - // } - // } + public string SelectedTheme + { + get => _settings.Theme; + set + { + if (_settings.Theme != value) + { + _settings.Theme = value; + OnPropertyChanged(); + _settings.Save(); + ThemeHelper.ApplyTheme(value); + } + } + } public List AvailableDbTypes { get; set; } - // public string SelectedDbType - // { - // get => _settings.Database.DbType; - // set - // { - // if (_settings.Database.DbType != value) - // { - // _settings.Database.DbType = value; - // OnPropertyChanged(); - // _settings.Save(); - // } - // } - // } + public string SelectedDbType + { + get => _settings.Database.DbType; + set + { + if (_settings.Database.DbType != value) + { + _settings.Database.DbType = value; + OnPropertyChanged(); + _settings.Save(); + } + } + } - // public string Server - // { - // get => _settings.Database.Server; - // set - // { - // if (_settings.Database.Server != value) - // { - // _settings.Database.Server = value; - // OnPropertyChanged(); - // _settings.Save(); - // } - // } - // } + public string Server + { + get => _settings.Database.Server; + set + { + if (_settings.Database.Server != value) + { + _settings.Database.Server = value; + OnPropertyChanged(); + _settings.Save(); + } + } + } - // public int Port - // { - // get => _settings.Database.Port; - // set - // { - // if (_settings.Database.Port != value) - // { - // _settings.Database.Port = value; - // OnPropertyChanged(); - // _settings.Save(); - // } - // } - // } + public int Port + { + get => _settings.Database.Port; + set + { + if (_settings.Database.Port != value) + { + _settings.Database.Port = value; + OnPropertyChanged(); + _settings.Save(); + } + } + } - // public string UserId - // { - // get => _settings.Database.UserId; - // set - // { - // if (_settings.Database.UserId != value) - // { - // _settings.Database.UserId = value; - // OnPropertyChanged(); - // _settings.Save(); - // } - // } - // } - // - // public string Password - // { - // get => _settings.Database.Password; - // set - // { - // if (_settings.Database.Password != value) - // { - // _settings.Database.Password = value; - // OnPropertyChanged(); - // _settings.Save(); - // } - // } - // } - // - // public string Database - // { - // get => _settings.Database.Database; - // set - // { - // if (_settings.Database.Database != value) - // { - // _settings.Database.Database = value; - // OnPropertyChanged(); - // _settings.Save(); - // } - // } - // } + public string UserId + { + get => _settings.Database.UserId; + set + { + if (_settings.Database.UserId != value) + { + _settings.Database.UserId = value; + OnPropertyChanged(); + _settings.Save(); + } + } + } + + public string Password + { + get => _settings.Database.Password; + set + { + if (_settings.Database.Password != value) + { + _settings.Database.Password = value; + OnPropertyChanged(); + _settings.Save(); + } + } + } + + public string Database + { + get => _settings.Database.Database; + set + { + if (_settings.Database.Database != value) + { + _settings.Database.Database = value; + OnPropertyChanged(); + _settings.Save(); + } + } + } - // public bool MinimizeToTrayOnClose - // { - // get => _settings.MinimizeToTrayOnClose; - // set - // { - // if (_settings.MinimizeToTrayOnClose != value) - // { - // _settings.MinimizeToTrayOnClose = value; - // OnPropertyChanged(nameof(MinimizeToTrayOnClose)); - // _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 = transaction.GetInstance()) - // { - // await db.Ado.OpenAsync(); - // NotificationHelper.ShowSuccess("连接成功!"); - // } - // } - // catch (Exception ex) - // { - // NotificationHelper.ShowError($"连接失败:{ex.Message}", ex); - // } + try + { + // 使用当前配置测试数据库连接 + using var db = new SqlSugar.SqlSugarScope(new SqlSugar.ConnectionConfig() + { + DbType = (SqlSugar.DbType)Enum.Parse(typeof(SqlSugar.DbType), _settings.Database.DbType), + ConnectionString = _settings.ToConnectionString(), + IsAutoCloseConnection = true + }); + + await db.Ado.ExecuteCommandAsync("SELECT 1"); + _notificationService.ShowSuccess("连接成功!"); + } + catch (Exception ex) + { + _notificationService.ShowError($"连接失败:{ex.Message}", ex); + } } } diff --git a/DMS.WPF/ViewModels/SplashViewModel.cs b/DMS.WPF/ViewModels/SplashViewModel.cs index 982b37c..3a2574a 100644 --- a/DMS.WPF/ViewModels/SplashViewModel.cs +++ b/DMS.WPF/ViewModels/SplashViewModel.cs @@ -8,6 +8,7 @@ using System; using System.Data; using System.Threading.Tasks; using DMS.Application.Services; +using DMS.Infrastructure.Configurations; using DMS.WPF.Helper; using DMS.WPF.Interfaces; using DMS.WPF.Views; @@ -26,18 +27,20 @@ public partial class SplashViewModel : ObservableObject private readonly IInitializeService _initializeService; private readonly IDataEventService _dataEventService; private readonly IAppDataCenterService _appDataCenterService; + private readonly AppSettings _appSettings; [ObservableProperty] private string _loadingMessage = "正在加载..."; public SplashViewModel(ILogger logger,IServiceProvider serviceProvider, IInitializeService initializeService,IDataEventService dataEventService, - IAppDataCenterService appDataCenterService) + IAppDataCenterService appDataCenterService,AppSettings appSettings) { _logger = logger; _serviceProvider = serviceProvider; _initializeService = initializeService; _dataEventService = dataEventService; this._appDataCenterService = appDataCenterService; + _appSettings = appSettings; } /// @@ -52,6 +55,8 @@ public partial class SplashViewModel : ObservableObject _initializeService.InitializeTables(); _initializeService.InitializeMenus(); LoadingMessage = "正在加载系统配置..."; + + await _appDataCenterService.DataLoaderService.LoadAllDataToMemoryAsync(); // 可以在这里添加加载配置的逻辑 diff --git a/DMS.WPF/Views/SettingView.xaml b/DMS.WPF/Views/SettingView.xaml index 6f13bf1..e0926a9 100644 --- a/DMS.WPF/Views/SettingView.xaml +++ b/DMS.WPF/Views/SettingView.xaml @@ -26,6 +26,11 @@ IsClickEnabled="True"> + + +