1- using Avalonia ;
1+ using Avalonia ;
22using Avalonia . Controls . ApplicationLifetimes ;
33using Avalonia . Data . Core . Plugins ;
44using Avalonia . Markup . Xaml ;
55using Modulus . App . Services ;
66using Modulus . App . ViewModels ;
77using Modulus . App . Views ;
8+ using Modulus . App . Options ;
89using Microsoft . Extensions . DependencyInjection ;
10+ using Microsoft . Extensions . Configuration ;
11+ using Microsoft . Extensions . Options ;
912using System ;
13+ using System . IO ;
1014
1115namespace Modulus . App ;
1216
1317public partial class App : Application
1418{
1519 private ServiceProvider ? _serviceProvider ;
20+ private IConfiguration ? _configuration ;
21+ private ConfigurationChangeListener ? _configChangeListener ;
1622
1723 public override void Initialize ( )
1824 {
@@ -21,53 +27,102 @@ public override void Initialize()
2127
2228 public override void OnFrameworkInitializationCompleted ( )
2329 {
24- // Line below is needed to remove Avalonia data validation.
25- // Without this line you will get duplicate validations from both Avalonia and CT
26- BindingPlugins . DataValidators . RemoveAt ( 0 ) ;
30+ try
31+ {
32+ // Line below is needed to remove Avalonia data validation.
33+ // Without this line you will get duplicate validations from both Avalonia and CT
34+ BindingPlugins . DataValidators . RemoveAt ( 0 ) ;
2735
28- // 配置服务
29- ConfigureServices ( ) ;
36+ // 配置服务
37+ ConfigureServices ( ) ;
3038
31- if ( ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
32- {
33- var mainViewModel = _serviceProvider ! . GetRequiredService < MainViewModel > ( ) ;
34- desktop . MainWindow = new MainWindow
39+ if ( ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
40+ {
41+ var mainViewModel = _serviceProvider ! . GetRequiredService < MainWindowViewModel > ( ) ;
42+ desktop . MainWindow = new MainWindow ( mainViewModel ) ;
43+
44+ // 启动配置更改监听器
45+ _configChangeListener = _serviceProvider . GetRequiredService < ConfigurationChangeListener > ( ) ;
46+
47+ desktop . Exit += ( s , e ) =>
48+ {
49+ _configChangeListener ? . Dispose ( ) ;
50+ ( _serviceProvider as IDisposable ) ? . Dispose ( ) ;
51+ } ;
52+ }
53+ else if ( ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform )
3554 {
36- DataContext = mainViewModel
37- } ;
55+ var mainViewModel = _serviceProvider ! . GetRequiredService < MainWindowViewModel > ( ) ;
56+ singleViewPlatform . MainView = new MainWindow ( mainViewModel ) ;
57+ }
58+
59+ base . OnFrameworkInitializationCompleted ( ) ;
3860 }
39- else if ( ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform )
61+ catch ( OptionsValidationException ex )
4062 {
41- // 在单视图平台中,也应该使用依赖注入获取MainViewModel
42- var mainViewModel = _serviceProvider ! . GetRequiredService < MainViewModel > ( ) ;
43- singleViewPlatform . MainView = new MainView
63+ // 处理配置验证错误
64+ System . Diagnostics . Debug . WriteLine ( $ "Configuration validation failed: { string . Join ( ", " , ex . Failures ) } " ) ;
65+ if ( ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
4466 {
45- DataContext = mainViewModel
46- } ;
67+ desktop . Shutdown ( 1 ) ;
68+ }
69+ throw ;
4770 }
48-
49- base . OnFrameworkInitializationCompleted ( ) ;
5071 }
5172
5273 private void ConfigureServices ( )
5374 {
75+ // 构建配置
76+ var builder = new ConfigurationBuilder ( )
77+ . SetBasePath ( AppContext . BaseDirectory )
78+ . AddJsonFile ( "appsettings.json" , optional : true , reloadOnChange : true )
79+ . AddJsonFile ( $ "appsettings.{ Environment . GetEnvironmentVariable ( "ASPNETCORE_ENVIRONMENT" ) ?? "Production" } .json", optional : true , reloadOnChange : true )
80+ . AddEnvironmentVariables ( "MODULUS_" )
81+ . AddUserSecrets < App > ( optional : true ) ;
82+
83+ _configuration = builder . Build ( ) ;
84+
5485 var services = new ServiceCollection ( ) ;
55-
86+
87+ // 注册配置服务
88+ services . AddSingleton < IConfiguration > ( _configuration ) ;
89+
90+ // 注册选项(支持热重载和验证)
91+ services . AddOptions < AppOptions > ( )
92+ . Bind ( _configuration . GetSection ( AppOptions . SectionName ) )
93+ . ValidateDataAnnotations ( )
94+ . ValidateOnStart ( ) ;
95+
96+ // 注册插件选项(支持热重载、验证和目录创建)
97+ services . AddOptions < PluginOptions > ( )
98+ . Bind ( _configuration . GetSection ( PluginOptions . SectionName ) )
99+ . ValidateOnStart ( ) ;
100+
101+ // 注册自定义选项验证器
102+ services . AddSingleton < IValidateOptions < PluginOptions > , PluginOptionsValidation > ( ) ;
103+
104+ // 注册配置更改监听器
105+ services . AddSingleton < ConfigurationChangeListener > ( ) ;
106+
56107 // 注册导航服务(单例)
57108 services . AddSingleton < INavigationService , NavigationService > ( ) ;
58-
109+
59110 // 注册导航插件服务(单例)
60111 services . AddSingleton < NavigationPluginService > ( ) ;
61-
112+
62113 // 注册插件管理器(单例)
63114 services . AddSingleton < IPluginManager , PluginManager > ( ) ;
64-
65- // 注册视图模型 (单例)
66- services . AddSingleton < MainViewModel > ( ) ;
67-
68- // 注册页面的ViewModel(可选择注册为单例或Transient )
115+
116+ // 注册主视图模型 (单例)
117+ services . AddSingleton < MainWindowViewModel > ( ) ;
118+
119+ // 注册所有页面的ViewModel(Transient )
69120 services . AddTransient < DashboardViewModel > ( ) ;
70-
121+ services . AddTransient < PluginManagerViewModel > ( ) ;
122+ services . AddTransient < SettingsViewModel > ( ) ;
123+ services . AddTransient < MainWindowViewModel > ( ) ;
124+ services . AddTransient < MainWindowViewModel > ( ) ;
125+
71126 // 创建服务提供器
72127 _serviceProvider = services . BuildServiceProvider ( ) ;
73128 }
0 commit comments