You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
4.5 KiB
108 lines
4.5 KiB
using Avalonia;
|
|
using Avalonia.Controls.ApplicationLifetimes;
|
|
using Avalonia.Markup.Xaml;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using MyAvaloniaApp.Configuration;
|
|
using MyAvaloniaApp.Extensions;
|
|
using MyAvaloniaApp.Views;
|
|
using ReactiveUI;
|
|
using Splat;
|
|
using System;
|
|
using System.IO;
|
|
|
|
namespace MyAvaloniaApp;
|
|
|
|
public partial class App : Application
|
|
{
|
|
private IServiceProvider? _serviceProvider;
|
|
private ILogger<App>? _logger;
|
|
|
|
/// <summary>
|
|
/// 无参构造函数,用于 Avalonia 框架初始化
|
|
/// </summary>
|
|
public App()
|
|
{
|
|
}
|
|
|
|
public override void Initialize()
|
|
{
|
|
AvaloniaXamlLoader.Load(this);
|
|
|
|
// 注册自定义 ViewLocator(必须在路由系统使用之前注册)
|
|
// 执行顺序:ViewLocator 先注册,ServiceProvider 后注册(在 OnFrameworkInitializationCompleted 中)
|
|
// ViewLocator 支持回退机制:如果 ServiceProvider 未注册,会使用 Activator.CreateInstance
|
|
// 因此先注册 ViewLocator 是安全的,即使此时 ServiceProvider 还未创建
|
|
Locator.CurrentMutable.RegisterConstant(new Views.ViewLocator(), typeof(IViewLocator));
|
|
}
|
|
|
|
public override void OnFrameworkInitializationCompleted()
|
|
{
|
|
// 在框架初始化完成后创建和配置 HostBuilder
|
|
// 执行顺序:ViewLocator 已在 Initialize() 中注册,现在创建 HostBuilder 和 ServiceProvider
|
|
// 虽然 ViewLocator 可以回退到 Activator.CreateInstance,但注册 ServiceProvider 后
|
|
// ViewLocator 将优先使用依赖注入创建 View,这样可以注入 View 所需的服务
|
|
var host = CreateHostBuilder().Build();
|
|
_serviceProvider = host.Services;
|
|
_logger = _serviceProvider.GetRequiredService<ILogger<App>>();
|
|
|
|
// 注册 ServiceProvider 到 Splat,以便 ViewLocator 可以使用依赖注入创建 View
|
|
// 必须在 MainWindow 创建和路由导航之前注册,因为路由导航会触发 ViewLocator.ResolveView
|
|
// 注册后,ViewLocator 将优先使用 DI 容器创建 View 实例
|
|
Locator.CurrentMutable.RegisterConstant(_serviceProvider, typeof(IServiceProvider));
|
|
|
|
_logger.LogInformation("App 框架初始化完成,HostBuilder 已创建并配置");
|
|
|
|
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
|
{
|
|
_logger.LogInformation("使用依赖注入创建 MainWindow");
|
|
desktop.MainWindow = _serviceProvider.GetRequiredService<MainWindow>();
|
|
}
|
|
|
|
base.OnFrameworkInitializationCompleted();
|
|
_logger.LogInformation("框架初始化完成");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建 HostBuilder
|
|
/// </summary>
|
|
/// <returns>HostBuilder</returns>
|
|
private IHostBuilder CreateHostBuilder() =>
|
|
Host.CreateDefaultBuilder()
|
|
.ConfigureAppConfiguration((context, config) =>
|
|
{
|
|
// 配置应用程序设置
|
|
config.SetBasePath(Directory.GetCurrentDirectory())
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true)
|
|
.AddEnvironmentVariables();
|
|
})
|
|
.ConfigureLogging((context, logging) =>
|
|
{
|
|
// 配置日志
|
|
logging.ClearProviders();
|
|
logging.AddConfiguration(context.Configuration.GetSection("Logging"));
|
|
// 启用控制台日志以便调试
|
|
logging.AddConsole();
|
|
logging.AddDebug();
|
|
})
|
|
.ConfigureServices((context, services) =>
|
|
{
|
|
// 注册配置
|
|
services.Configure<AppSettings>(context.Configuration.GetSection("AppSettings"));
|
|
|
|
// 注册业务服务
|
|
services.AddBusinessServices();
|
|
|
|
// 注册 ReactiveUI 服务(必须在注册 ViewModel 之前,因为 ViewModel 依赖 AppViewModel)
|
|
services.AddReactiveUI();
|
|
|
|
// 注册 ViewModel(依赖 AppViewModel)
|
|
services.AddViewModels();
|
|
|
|
// 注册 Avalonia 应用程序
|
|
services.AddTransient<MainWindow>();
|
|
});
|
|
}
|