添加ConfigUtils相关内容

This commit is contained in:
SharwOrange橙夜 2024-07-15 17:02:53 +08:00
parent 33011fc748
commit 89916dd828
Signed by: SharwOrange
GPG Key ID: 5507630E4F33F4B1
9 changed files with 142 additions and 18 deletions

View File

@ -2,12 +2,10 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="toast_mcl.App" x:Class="toast_mcl.App"
xmlns:sty="using:FluentAvalonia.Styling" xmlns:sty="using:FluentAvalonia.Styling"
RequestedThemeVariant="Dark" RequestedThemeVariant="Dark">
>
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. --> <!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
<Application.Styles> <Application.Styles>
<!--<FluentTheme />-->
<sty:FluentAvaloniaTheme /> <sty:FluentAvaloniaTheme />
</Application.Styles> </Application.Styles>
<Application.Resources> <Application.Resources>

View File

@ -8,11 +8,6 @@
Title="toast_mcl"> Title="toast_mcl">
<Grid> <Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TabControl> <TabControl>
<TabItem Header="主页"> <TabItem Header="主页">
<ContentControl> <ContentControl>
@ -40,7 +35,6 @@
<local:About/> <local:About/>
</ContentControl> </ContentControl>
</TabItem> </TabItem>
</TabControl> </TabControl>
</Grid> </Grid>
</Window> </Window>

View File

@ -1,5 +1,6 @@
<UserControl xmlns="https://github.com/avaloniaui" <UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="toast_mcl.About"> x:Class="toast_mcl.About">
This is About <UniformGrid Columns="1" Rows="1" HorizontalAlignment="Center" VerticalAlignment="Center">
</UniformGrid>
</UserControl> </UserControl>

View File

@ -1,23 +1,31 @@
using Avalonia; using Avalonia;
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using toast_mcl;
namespace toast_mcl namespace toast_mcl
{ {
internal class Program internal class Program
{ {
// Initialization code. Don't use any Avalonia, third-party APIs or any // 初始化代码。
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized // 在调用AppMain之前请不要使用任何Avalonia、第三方API或任何依赖于SynchronizationContext的代码
// yet and stuff might break. // 因为这些东西尚未初始化,可能会导致错误。
[STAThread] [STAThread]
public static void Main(string[] args) => BuildAvaloniaApp() public static void Main(string[] args)
.StartWithClassicDesktopLifetime(args); {
Base.Init();
// Avalonia configuration, don't remove; also used by visual designer. BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
}
// Avalonia配置请勿删除也被可视化设计器使用。
public static AppBuilder BuildAvaloniaApp() public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>() {
return AppBuilder.Configure<App>()
.UsePlatformDetect() .UsePlatformDetect()
.WithFont_LXGWWenKai() .WithFont_LXGWWenKai()
.LogToTrace(); .LogToTrace();
}
} }
} }

26
Programs/Base.cs Normal file
View File

@ -0,0 +1,26 @@
using System;
using toast_mcl.Programs;
namespace toast_mcl;
public static class Base
{
// 版本号处理
public enum VersionTypes
{
Alpha,
Beta,
Release
}
public static VersionTypes VersionType = VersionTypes.Alpha;
public static Version Version = new Version(0, 0, 1);
// 获取程序目录
public static string AppDirectory = AppDomain.CurrentDomain.BaseDirectory;
// 初始化
public static void Init()
{
ConfigUtils.Init();
}
}

View File

@ -0,0 +1,15 @@
using System;
using toast_mcl;
namespace toast_mcl.Config;
public class ConfigSettings
{
public AppSettings ?AppSettings { get; set; }
}
public class AppSettings
{
public Version Version { get; set; } = Base.Version;
public string VersionType { get; set; } = Base.VersionType.ToString();
}

View File

@ -0,0 +1,60 @@
using System.Linq;
using System.IO;
using Newtonsoft.Json;
using toast_mcl.Config;
namespace toast_mcl.Programs;
public static class ConfigUtils
{
public static ConfigSettings ?Settings { get; private set;}
private static readonly string ConfigFolderPath = Path.Combine(Base.AppDirectory, "toast_mc");
private static readonly string ConfigFilePath = Path.Combine(ConfigFolderPath, "config.json");
public static void Init()
{
if (!Directory.Exists(ConfigFolderPath))
{
Directory.CreateDirectory(ConfigFolderPath);
}
if (!File.Exists(ConfigFilePath))
{
CreateDefaultConfig();
}
}
private static void CreateDefaultConfig()
{
Settings = DefaultConfig.GetDefaultConfig();
SaveConfig();
}
public static void LoadConfig()
{
if (File.Exists(ConfigFilePath))
{
string configJson = File.ReadAllText(ConfigFilePath);
Settings = JsonConvert.DeserializeObject<ConfigSettings>(configJson);
}
else
{
CreateDefaultConfig();
}
var ConfigVersion = Settings?.AppSettings?.Version;
if (ConfigVersion != Base.Version)
{
var oldSettings = Settings;
UpdateConfig();
}
}
private static void UpdateConfig()
{
}
public static void SaveConfig()
{
string configJson = JsonConvert.SerializeObject(Settings, Formatting.Indented);
File.WriteAllText(ConfigFilePath, configJson);
}
}

View File

@ -0,0 +1,20 @@
using System;
using toast_mcl;
namespace toast_mcl.Config;
public static class DefaultConfig
{
public static ConfigSettings GetDefaultConfig()
{
return new ConfigSettings
{
AppSettings = new AppSettings
{
Version = Base.Version,
VersionType = Base.VersionType.ToString()
},
};
}
}

View File

@ -25,6 +25,8 @@
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.6" /> <PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.6" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.--> <!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.6" /> <PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.6" />
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="0.5.0" />
<PackageReference Include="FluentAvaloniaUI" Version="2.0.5" /> <PackageReference Include="FluentAvaloniaUI" Version="2.0.5" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup> </ItemGroup>
</Project> </Project>