feat: 初始化NewLife Studio项目,完成基础框架与数据管理模块
何炳宏 authored at 2026-05-26 12:09:09
1.96 KiB
NewLife.Studio
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core.Plugins;
using Microsoft.Extensions.DependencyInjection;
using NewLife.Studio.App.Services;
using NewLife.Studio.App.Views;
using NewLife.Studio.Core;
using NewLife.Studio.Data;
using NewLife.Studio.Data.Providers.SQLite;
using NewLife.Studio.Store;
// 触发 DataStudio 模块程序集加载,确保 ModuleLoader 能扫描到
using NewLife.Studio.Modules.DataStudio;

namespace NewLife.Studio.App;

public partial class App : Application
{
    private ServiceProvider? _serviceProvider;

    public IServiceProvider? GetServiceProvider() => _serviceProvider;

    public override void Initialize()
    {
        Avalonia.Markup.Xaml.AvaloniaXamlLoader.Load(this);
    }

    public override void OnFrameworkInitializationCompleted()
    {
        var services = new ServiceCollection();
        ConfigureServices(services);
        _serviceProvider = services.BuildServiceProvider();
        StudioServices.Initialize(_serviceProvider);

        // 强制加载模块程序集
        _ = typeof(DataStudioModule).FullName;

        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            var mainWindow = _serviceProvider.GetRequiredService<MainWindow>();
            desktop.MainWindow = mainWindow;

            mainWindow.InitializeAsync().ContinueWith(t =>
            {
                if (t.Exception != null)
                {
                    NewLife.Log.XTrace.WriteLine($"MainWindow init error: {t.Exception}");
                }
            });
        }

        base.OnFrameworkInitializationCompleted();
    }

    private static void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<ModuleLoader>();
        services.AddSingleton<MainWindow>();
        services.AddSingleton<IStoreService, StoreService>();
        services.AddSingleton<IDataProvider, SQLiteProvider>();
        services.AddSingleton<HttpClient>();
    }
}