- 新增 package-scripts 目录,包含 Inno Setup 安装包打包脚本 - 在 Avalonia-PC.csproj 中配置应用程序图标 - 更新 .gitignore 忽略打包输出目录和工具目录
98 lines
3.5 KiB
C#
98 lines
3.5 KiB
C#
using Authentication;
|
||
using Avalonia;
|
||
using Avalonia_Common.Infrastructure;
|
||
using Avalonia_EFCore.Database;
|
||
using Avalonia_PC.Authentication;
|
||
using Avalonia_PC.Views;
|
||
using Avalonia_Services.Core;
|
||
using Avalonia_Services.Endpoints;
|
||
using Avalonia_Services.Services;
|
||
using Avalonia_Services.Services.AuthService;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using Serilog;
|
||
using System;
|
||
|
||
namespace Avalonia_PC
|
||
{
|
||
/// <summary>
|
||
/// 桌面应用程序入口类,负责配置 DI 容器、初始化数据库和启动 Avalonia 框架。
|
||
/// </summary>
|
||
internal sealed class Program
|
||
{
|
||
/// <summary>
|
||
/// 获取全局 DI 服务提供程序。
|
||
/// </summary>
|
||
public static IServiceProvider Services { get; private set; } = null!;
|
||
|
||
/// <summary>
|
||
/// 应用程序主入口点。
|
||
/// </summary>
|
||
/// <param name="args">命令行参数。</param>
|
||
[STAThread]
|
||
public static void Main(string[] args)
|
||
{
|
||
// 初始化日志系统
|
||
AppLog.Initialize(LoggingConfiguration.CreateDefaultLogger(logDir: "logs"));
|
||
|
||
AppLog.Information("Avalonia-PC 正在启动...");
|
||
|
||
ConfigureServices();
|
||
|
||
// 初始化数据库(自动迁移 + 种子数据)
|
||
Services.InitializeDatabase<AppDataContext>();
|
||
|
||
#if DEBUG
|
||
// 开启 WebView2 远程调试,启动后在 Edge 中访问 edge://inspect 调试网页
|
||
Environment.SetEnvironmentVariable(
|
||
"WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS",
|
||
"--remote-debugging-port=9222 --auto-open-devtools-for-tabs");
|
||
#endif
|
||
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 配置 DI 容器,注册数据库、业务服务、鉴权服务和统一端点。
|
||
/// </summary>
|
||
private static void ConfigureServices()
|
||
{
|
||
var services = new ServiceCollection();
|
||
|
||
// ---- 数据库 ----
|
||
// 注册默认数据库提供程序(SQLite / MySQL / PostgreSQL / SqlServer)
|
||
DatabaseProviderRegistry.RegisterDefaults();
|
||
|
||
// 桌面端固定使用 SQLite 本地数据库
|
||
services.AddAppDatabase<AppDataContext>(DatabaseConfiguration.ForSQLite("app.db"));
|
||
|
||
// ---- 业务服务 ----
|
||
services.AddSingleton<WeatherForecastService>();
|
||
services.AddSingleton<IPcThirdPartyAuthorizationClient, DefaultPcThirdPartyAuthorizationClient>();
|
||
services.AddSingleton<PcGlobalTokenService>();
|
||
services.AddSingleton<IAuthService, PcAuthService>();
|
||
services.AddSingleton<IPcAuthEndpointService, PcAuthEndpointService>();
|
||
|
||
// ---- 端点注册 ----
|
||
var endpointBuilder = new ServiceEndpointBuilder();
|
||
AppEndpoints.Configure(endpointBuilder);
|
||
AuthEndpoints.ConfigurePc(endpointBuilder);
|
||
var endpoints = endpointBuilder.Build();
|
||
services.AddSingleton(endpoints);
|
||
|
||
// 注册 Window
|
||
services.AddTransient<MainWindow>(sp => new MainWindow(sp));
|
||
|
||
Services = services.BuildServiceProvider();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 构建 Avalonia 应用程序(供可视化设计器使用,请勿删除)。
|
||
/// </summary>
|
||
/// <returns>Avalonia 应用构建器。</returns>
|
||
public static AppBuilder BuildAvaloniaApp()
|
||
=> AppBuilder.Configure<App>()
|
||
.UsePlatformDetect()
|
||
.WithInterFont()
|
||
.LogToTrace();
|
||
}
|
||
}
|