1- using Newtonsoft . Json ;
1+ using CommunityToolkit . Mvvm . DependencyInjection ;
2+ using CommunityToolkit . Mvvm . Messaging ;
3+ using Microsoft . Extensions . DependencyInjection ;
4+ using Newtonsoft . Json ;
25using System ;
36using System . Collections . Generic ;
47using System . Configuration ;
58using System . Data ;
69using System . Windows ;
710using System . Windows . Threading ;
8- using GalaSoft . MvvmLight . Messaging ;
9- using GalaSoft . MvvmLight . Threading ;
1011using Workshop . Common ;
1112using Workshop . Helper ;
12-
13+ using Workshop . ViewModel ;
1314
1415namespace Workshop
1516{
@@ -18,9 +19,24 @@ namespace Workshop
1819 /// </summary>
1920 public partial class App : Application
2021 {
22+ private bool _initialized ;
23+
2124 public static string Session ;
2225 public App ( )
2326 {
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+ . BuildServiceProvider ( ) ) ;
38+ }
39+
2440 App . Current . Startup += Current_Startup ;
2541 App . Current . Exit += Current_Exit ;
2642
@@ -41,7 +57,7 @@ private void App_OnDispatcherUnhandledException(object sender, DispatcherUnhandl
4157 {
4258 try
4359 {
44- Messenger . Default . Send ( "" , MessengerToken . CLOSEPROGRESS ) ;
60+ WeakReferenceMessenger . Default . Send ( MessengerToken . CLOSEPROGRESS ) ;
4561
4662 LogHelper . LogError ( "UI线程全局异常" + e . Exception ) ;
4763 MessageBox . Show ( "An unhandled exception just occurred: " + e . Exception . Message , "UI线程全局异常" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
@@ -63,7 +79,7 @@ private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionE
6379 {
6480 try
6581 {
66- Messenger . Default . Send ( "" , MessengerToken . CLOSEPROGRESS ) ;
82+ WeakReferenceMessenger . Default . Send ( MessengerToken . CLOSEPROGRESS ) ;
6783
6884 var exception = e . ExceptionObject as Exception ;
6985 if ( exception != null )
@@ -85,7 +101,6 @@ private void Current_Startup(object sender, StartupEventArgs e)
85101 Current . DispatcherUnhandledException += App_OnDispatcherUnhandledException ;
86102 AppDomain . CurrentDomain . UnhandledException += CurrentDomain_UnhandledException ;
87103 LogHelper . LogFlag = true ;
88- DispatcherHelper . Initialize ( ) ;
89104
90105 }
91106 }
0 commit comments