55using System . Data ;
66using System . Windows ;
77using System . Windows . Threading ;
8- using GalaSoft . MvvmLight . Messaging ;
98using Workshop . Common ;
109using Workshop . Helper ;
11-
10+ using CommunityToolkit . Mvvm . DependencyInjection ;
11+ using Microsoft . Extensions . DependencyInjection ;
12+ using Workshop . ViewModel ;
13+ using CommunityToolkit . Mvvm . Messaging ;
1214
1315namespace Workshop
1416{
@@ -17,9 +19,26 @@ namespace Workshop
1719 /// </summary>
1820 public partial class App : Application
1921 {
22+ private bool _initialized ;
23+
2024 public static string Session ;
2125 public App ( )
2226 {
27+ // Register services
28+ if ( ! _initialized )
29+ {
30+ _initialized = true ;
31+ Ioc . Default . ConfigureServices (
32+ new ServiceCollection ( )
33+ //ViewModels
34+ . AddSingleton < MainViewModel > ( )
35+ . AddSingleton < IndexPageViewModel > ( )
36+ . AddSingleton < SettingPageViewModel > ( )
37+ . AddSingleton < BatchProcessViewModel > ( )
38+ . BuildServiceProvider ( ) ) ;
39+ }
40+
41+
2342 App . Current . Startup += Current_Startup ;
2443 App . Current . Exit += Current_Exit ;
2544
@@ -40,7 +59,7 @@ private void App_OnDispatcherUnhandledException(object sender, DispatcherUnhandl
4059 {
4160 try
4261 {
43- Messenger . Default . Send ( "" , MessengerToken . CLOSEPROGRESS ) ;
62+ WeakReferenceMessenger . Default . Send ( MessengerToken . CLOSEPROGRESS ) ;
4463
4564 LogHelper . LogError ( "UI线程全局异常" + e . Exception ) ;
4665 MessageBox . Show ( "An unhandled exception just occurred: " + e . Exception . Message , "UI线程全局异常" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
@@ -62,7 +81,7 @@ private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionE
6281 {
6382 try
6483 {
65- Messenger . Default . Send ( "" , MessengerToken . CLOSEPROGRESS ) ;
84+ WeakReferenceMessenger . Default . Send ( MessengerToken . CLOSEPROGRESS ) ;
6685
6786 var exception = e . ExceptionObject as Exception ;
6887 if ( exception != null )
0 commit comments