From d3f7d2e07c4aadad42a9c26dcb27495a5e83ec42 Mon Sep 17 00:00:00 2001 From: czz_y Date: Mon, 9 Sep 2024 08:46:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=EF=BC=8C?= =?UTF-8?q?=E5=B0=81=E8=A3=85json=E8=AF=BB=E5=8F=96=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MainWindow.cs | 11 +++-------- Utils/AppSetting.cs | 2 +- Utils/ConfigurationHelper.cs | 25 +++++++++++++++++++++++++ Utils/LoggerHelper.cs | 2 +- Utils/ThemeHelper.cs | 2 +- 5 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 Utils/ConfigurationHelper.cs diff --git a/MainWindow.cs b/MainWindow.cs index 61193c9..7f0d5b8 100644 --- a/MainWindow.cs +++ b/MainWindow.cs @@ -48,10 +48,7 @@ namespace AntdUIDemo private void LoadAppConfig() { // 读取 appsettings.json 文件 - var configFilePath = "appsettings.json"; - var json = File.ReadAllText(configFilePath); - var jsonObj = JObject.Parse(json); - var appSettings = jsonObj["AppSettings"]; + var appSettings = ConfigurationHelper.GetAppSettings(); // 加载色彩模式 var value = appSettings["ColorMode"]?.ToString(); @@ -88,10 +85,7 @@ namespace AntdUIDemo private void Button_color_Click(object sender, EventArgs e) { // 读取 appsettings.json 文件 - var configFilePath = "appsettings.json"; - var json = File.ReadAllText(configFilePath); - var jsonObj = JObject.Parse(json); - var appSettings = jsonObj["AppSettings"]; + var appSettings = ConfigurationHelper.GetAppSettings(); var value = appSettings["ColorMode"]?.ToString(); if (value == "Auto") { @@ -314,6 +308,7 @@ namespace AntdUIDemo } if (control != null) { + //容器添加控件,需要调整dpi AutoDpi(control); panel_content.Controls.Add(control); } diff --git a/Utils/AppSetting.cs b/Utils/AppSetting.cs index 4ee3bbb..b2d1a15 100644 --- a/Utils/AppSetting.cs +++ b/Utils/AppSetting.cs @@ -9,7 +9,7 @@ using System.Threading.Tasks; namespace AntdUIDemo.Utils { - public static class AppSetting + public class AppSetting { private static readonly string configFilePath = "appsettings.json"; diff --git a/Utils/ConfigurationHelper.cs b/Utils/ConfigurationHelper.cs new file mode 100644 index 0000000..3a251cd --- /dev/null +++ b/Utils/ConfigurationHelper.cs @@ -0,0 +1,25 @@ +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntdUIDemo.Utils +{ + public class ConfigurationHelper + { + public static JObject GetAppSettings(string configFilePath = "appsettings.json") + { + // Read the JSON file + var json = File.ReadAllText(configFilePath); + + // Parse the JSON object + var jsonObj = JObject.Parse(json); + + // Return the "AppSettings" section + return (JObject)jsonObj["AppSettings"]; + } + } +} diff --git a/Utils/LoggerHelper.cs b/Utils/LoggerHelper.cs index 7dc67f4..980b136 100644 --- a/Utils/LoggerHelper.cs +++ b/Utils/LoggerHelper.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace AntdUIDemo.Utils { - public static class LoggerHelper + public class LoggerHelper { public static readonly Logger Logger = LogManager.GetCurrentClassLogger(); } diff --git a/Utils/ThemeHelper.cs b/Utils/ThemeHelper.cs index 4e71bb6..4d3afd8 100644 --- a/Utils/ThemeHelper.cs +++ b/Utils/ThemeHelper.cs @@ -9,7 +9,7 @@ using Microsoft.Win32; namespace AntdUIDemo.Utils { - public static class ThemeHelper + public class ThemeHelper { public static bool IsLightMode() {