refactor:规范代码格式和注释
This commit is contained in:
@@ -10,8 +10,14 @@ using Hua.Todo.Maui.Services.Platforms;
|
||||
|
||||
namespace Hua.Todo.Maui;
|
||||
|
||||
/// <summary>
|
||||
/// MAUI 程序启动类
|
||||
/// </summary>
|
||||
public static class MauiProgram
|
||||
{
|
||||
/// <summary>
|
||||
/// 创建并配置 MAUI 应用程序
|
||||
/// </summary>
|
||||
public static MauiApp CreateMauiApp()
|
||||
{
|
||||
var builder = MauiApp.CreateBuilder();
|
||||
@@ -23,9 +29,10 @@ public static class MauiProgram
|
||||
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
|
||||
});
|
||||
|
||||
// 加载应用程序配置
|
||||
var appSettings = LoadAppSettings();
|
||||
|
||||
// Set default connection string if not provided
|
||||
// 如果未提供连接字符串,则设置默认的 SQLite 连接字符串
|
||||
if (string.IsNullOrEmpty(appSettings.WebServer.ConnectionString))
|
||||
{
|
||||
var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
||||
@@ -41,12 +48,18 @@ public static class MauiProgram
|
||||
builder.Services.AddSingleton(appSettings);
|
||||
var connectionString = appSettings.WebServer.ConnectionString;
|
||||
|
||||
// 注册应用服务
|
||||
builder.Services.AddApplicationServices(connectionString);
|
||||
builder.Services.AddTransient<Views.MainPage>();
|
||||
|
||||
// 注册热键设置服务
|
||||
builder.Services.AddSingleton<IHotKeySettingsService>(sp =>
|
||||
new HotKeySettingsService(sp.GetRequiredService<AppSettings>()));
|
||||
|
||||
// 注册全局热键服务(工厂模式)
|
||||
builder.Services.AddSingleton<IGlobalHotKeyService>(sp => GlobalHotKeyServiceFactory.Create());
|
||||
|
||||
// 注册系统托盘服务(平台相关)
|
||||
builder.Services.AddSingleton<ISystemTrayService>(sp =>
|
||||
{
|
||||
#if WINDOWS
|
||||
@@ -55,6 +68,8 @@ public static class MauiProgram
|
||||
return new NullSystemTrayService();
|
||||
#endif
|
||||
});
|
||||
|
||||
// 注册嵌入式 Web 服务器(平台相关)
|
||||
#if WINDOWS
|
||||
builder.Services.AddSingleton<IEmbeddedWebServerService, EmbeddedWebServerService>();
|
||||
#elif ANDROID
|
||||
@@ -69,6 +84,7 @@ public static class MauiProgram
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// 异步初始化数据库和 Web 服务器
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
@@ -85,6 +101,9 @@ public static class MauiProgram
|
||||
return app;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从 appsettings.json 加载配置
|
||||
/// </summary>
|
||||
private static AppSettings LoadAppSettings()
|
||||
{
|
||||
try
|
||||
@@ -114,6 +133,9 @@ public static class MauiProgram
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化数据库(执行迁移)
|
||||
/// </summary>
|
||||
private static void InitializeDatabase(IServiceProvider services, string connectionString)
|
||||
{
|
||||
using var scope = services.CreateScope();
|
||||
@@ -133,7 +155,7 @@ public static class MauiProgram
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure WAL mode to avoid locking issues
|
||||
// 确保使用 WAL 模式以避免锁定问题
|
||||
dbContext.Database.ExecuteSqlRaw("PRAGMA journal_mode=WAL;");
|
||||
dbContext.Database.Migrate();
|
||||
}
|
||||
@@ -152,6 +174,9 @@ public static class MauiProgram
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 启动嵌入式 Web 服务器
|
||||
/// </summary>
|
||||
private static async Task StartWebServer(IServiceProvider services)
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user