11// Disable XAML Generated break on unhalted exception
22// <seealso href="" />
3- #define DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
3+ // #define DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
44
55
6- using System . Text ;
76using CommunityToolkit . WinUI ;
87using electrifier . Activation ;
98using electrifier . Contracts . Services ;
9+ using electrifier . Models . Configuration . Global ;
1010using electrifier . Models ;
11- using electrifier . Notifications ;
1211using electrifier . Services ;
1312using electrifier . ViewModels ;
1413using electrifier . Views ;
15-
16- using Microsoft . AppCenter ;
1714using Microsoft . AppCenter . Analytics ;
1815using Microsoft . AppCenter . Crashes ;
16+ using Microsoft . AppCenter ;
1917using Microsoft . Extensions . DependencyInjection ;
2018using Microsoft . Extensions . Hosting ;
2119using Microsoft . UI . Xaml ;
20+ using System . Diagnostics ;
21+ using System . Text ;
2222
2323namespace electrifier ;
2424
@@ -35,15 +35,26 @@ public IHost Host
3535 get ;
3636 }
3737
38- public static T GetService < T > ( )
38+ public static T ? GetService < T > ( )
3939 where T : class
4040 {
41- if ( ( App . Current as App ) ! . Host . Services . GetService ( typeof ( T ) ) is not T service )
41+ try
42+ {
43+ if ( ( App . Current as App ) ! . Host . Services . GetService ( typeof ( T ) ) is not T service )
44+ {
45+ throw new ArgumentException (
46+ $ "{ typeof ( T ) } needs to be registered in ConfigureServices within App.xaml.cs.") ;
47+ }
48+
49+ return service ;
50+ }
51+ catch ( Exception ex )
4252 {
43- throw new ArgumentException ( $ "{ typeof ( T ) } needs to be registered in ConfigureServices within App.xaml.cs.") ;
53+ Debug . WriteLine ( ex . Message ) ;
54+ Debug . WriteLine ( ex . StackTrace ) ;
4455 }
4556
46- return service ;
57+ return null ;
4758 }
4859
4960 public static WindowEx MainWindow { get ; } = new MainWindow ( ) ;
@@ -58,72 +69,69 @@ public App()
5869 CreateDefaultBuilder ( ) .
5970 UseContentRoot ( AppContext . BaseDirectory ) .
6071 ConfigureServices ( ( context , services ) =>
61- {
62- // Default Activation Handler
63- _ = services . AddTransient < ActivationHandler < LaunchActivatedEventArgs > , DefaultActivationHandler > ( ) ;
64-
65- // Other Activation Handlers
66- services . AddTransient < IActivationHandler , AppNotificationActivationHandler > ( ) ;
67-
68- // Services
69- services . AddSingleton < IAppNotificationService , AppNotificationService > ( ) ;
70- services . AddSingleton < ILocalSettingsService , LocalSettingsService > ( ) ;
71- services . AddSingleton < IThemeSelectorService , ThemeSelectorService > ( ) ;
72- services . AddTransient < INavigationViewService , NavigationViewService > ( ) ;
73- services . AddTransient < IWebViewService , WebViewService > ( ) ;
74-
75- services . AddSingleton < IActivationService , ActivationService > ( ) ;
76- services . AddSingleton < INavigationService , NavigationService > ( ) ;
77- services . AddSingleton < IPageService , PageService > ( ) ;
78-
79- //// Core Services
80- services . AddSingleton < IFileService , FileService > ( ) ;
81-
82- // Views and ViewModels
83- services . AddTransient < ClipboardPage > ( ) ;
84- services . AddTransient < ClipboardViewModel > ( ) ;
85- services . AddTransient < DevicesPage > ( ) ;
86- services . AddTransient < DevicesViewModel > ( ) ;
87- services . AddTransient < FileManagerPage > ( ) ;
88- services . AddTransient < FileManagerViewModel > ( ) ;
89- services . AddTransient < NetworkDevicesPage > ( ) ;
90- services . AddTransient < NetworkDevicesViewModel > ( ) ;
91- services . AddTransient < SettingsPage > ( ) ;
92- services . AddTransient < SettingsViewModel > ( ) ;
93- services . AddTransient < ShellPage > ( ) ;
94- services . AddTransient < ShellViewModel > ( ) ;
95- services . AddTransient < WebFavoritesPage > ( ) ;
96- services . AddTransient < WebFavoritesViewModel > ( ) ;
97- services . AddTransient < WebHostsPage > ( ) ;
98- services . AddTransient < WebHostsViewModel > ( ) ;
99- services . AddTransient < WebViewPage > ( ) ;
100- services . AddTransient < WebViewViewModel > ( ) ;
101- services . AddTransient < WorkbenchPage > ( ) ;
102- services . AddTransient < WorkbenchViewModel > ( ) ;
103-
104- // Configuration
105- services . Configure < LocalSettingsOptions > ( context . Configuration . GetSection ( nameof ( LocalSettingsOptions ) ) ) ;
106- } ) . Build ( ) ;
107-
108- App . GetService < IAppNotificationService > ( ) . Initialize ( ) ;
109-
110- // AppCenter.Start("{Your app secret here}", typeof(Analytics), typeof(Crashes));
111-
112- // App_UnhandledException(this, new Microsoft.UI.Xaml.UnhandledExceptionEventArgs(this, false));
113- //System.UnhandledExceptionEventArgs args = new System.UnhandledExceptionEventArgs();
114-
115- //var v1 = new Microsoft.UI.Xaml.UnhandledExceptionEventArgs();
116-
117- // UnhandledException.UnhandledException += App_UnhandledException;
118-
72+ {
73+ // Default Activation Handler
74+ _ = services . AddTransient < ActivationHandler < LaunchActivatedEventArgs > , DefaultActivationHandler > ( ) ;
75+
76+ // Other Activation Handlers
77+ services . AddTransient < IActivationHandler , AppNotificationActivationHandler > ( ) ;
78+
79+ // Services
80+ services . AddSingleton < IAppNotificationService , AppNotificationService > ( ) ;
81+ services . AddSingleton < ILocalSettingsService , LocalSettingsService > ( ) ;
82+ services . AddSingleton < IThemeSelectorService , ThemeSelectorService > ( ) ;
83+ services . AddTransient < INavigationViewService , NavigationViewService > ( ) ;
84+ services . AddTransient < IWebViewService , WebViewService > ( ) ;
85+
86+ services . AddSingleton < IActivationService , ActivationService > ( ) ;
87+ services . AddSingleton < INavigationService , NavigationService > ( ) ;
88+ services . AddSingleton < IPageService , PageService > ( ) ;
89+
90+ // Core Services
91+ services . AddSingleton < IFileService , FileService > ( ) ;
92+
93+ // Views and ViewModels
94+ services . AddTransient < ClipboardPage > ( ) ;
95+ services . AddTransient < ClipboardViewModel > ( ) ;
96+ services . AddTransient < DevicesPage > ( ) ;
97+ services . AddTransient < DevicesViewModel > ( ) ;
98+ services . AddTransient < FileManagerPage > ( ) ;
99+ services . AddTransient < FileManagerViewModel > ( ) ;
100+ services . AddTransient < NetworkDevicesPage > ( ) ;
101+ services . AddTransient < NetworkDevicesViewModel > ( ) ;
102+ services . AddTransient < SettingsPage > ( ) ;
103+ services . AddTransient < SettingsViewModel > ( ) ;
104+ services . AddTransient < ShellPage > ( ) ;
105+ services . AddTransient < ShellViewModel > ( ) ;
106+ services . AddTransient < WebFavoritesPage > ( ) ;
107+ services . AddTransient < WebFavoritesViewModel > ( ) ;
108+ services . AddTransient < WebHostsPage > ( ) ;
109+ services . AddTransient < WebHostsViewModel > ( ) ;
110+ services . AddTransient < WebViewPage > ( ) ;
111+ services . AddTransient < WebViewViewModel > ( ) ;
112+ services . AddTransient < WorkbenchPage > ( ) ;
113+ services . AddTransient < WorkbenchViewModel > ( ) ;
114+
115+ // Configuration
116+ services . Configure < LocalSettingsOptions > ( context . Configuration . GetSection ( nameof ( LocalSettingsOptions ) ) ) ;
117+ } ) . Build ( ) ;
118+
119+ GetService < IAppNotificationService > ( ) ?
120+ . Initialize ( ) ;
121+
122+ UnhandledException += App_UnhandledException ;
119123 }
120124
121- /// <summary>
122- ///
123- /// </summary>
124- /// <param name="sender"></param>
125- /// <param name="args"></param>
126- private void App_UnhandledException ( object sender , Microsoft . UI . Xaml . UnhandledExceptionEventArgs args ) => App_UnhandledException ( sender , args , false ) ;
125+ //private void App_StartAppCenter()
126+ //{
127+ // AppCenter.Start("{ TODO:Your_app_secret_here }", typeof(Analytics), typeof(Crashes));
128+ //}
129+
130+
131+ private void App_UnhandledException ( object sender , Microsoft . UI . Xaml . UnhandledExceptionEventArgs args )
132+ {
133+ App_UnhandledException ( sender , args , false ) ;
134+ }
127135
128136 /// <summary>
129137 /// Log and handle exceptions as appropriate.
@@ -139,41 +147,24 @@ public App()
139147 /// <param name="itIsComplicated">
140148 /// Set <b>true</b> to force shutdown in case of critical error.<br/>
141149 /// <br/>
142- /// Triggers <see cref="UnhandledExceptionEventArgs.Handled"/>.
150+ /// Triggers <see cref="Microsoft.UI.Xaml. UnhandledExceptionEventArgs.Handled"/>.
143151 /// </param>
144152 private void App_UnhandledException (
145153 object sender ,
146154 Microsoft . UI . Xaml . UnhandledExceptionEventArgs args ,
147- bool itIsComplicated = false )
155+ bool itIsComplicated )
148156 {
149157 try
150158 {
159+ StringBuilder sb = new ( ) ;
160+
161+ sb . AppendJoin ( "\n " , "Exception happened!" , "line 1" , "line 2" ) ;
162+
151163 // TODO: Try to make an backup of current configuration and mark as "dirty".
152164 // TODO: Log and handle exceptions as appropriate.
153165
154- //var guru = App.GetService<GuruMeditationDialoge>();
155-
156166 //guru?.ThrowGuruMeditation(sender, args);
157167
158-
159-
160- /*
161- var stringBuilder = new StringBuilder()
162- .AppendLine("GURU MEDITATION")
163- .AppendLine("Sender: { }")
164- .AppendLine(args?.ToString());
165- */
166-
167-
168-
169- //if (args is not null)
170- //{
171- // // TODO: Exception happened!
172-
173- // args.Handled = true;
174- //}
175-
176-
177168 /*
178169
179170 private async void ShowDialog_Click(object sender, RoutedEventArgs e)
@@ -214,23 +205,25 @@ private async void ShowDialog_Click(object sender, RoutedEventArgs e)
214205 }
215206 catch ( Exception ex )
216207 {
217- ex . ToString ( ) ;
208+ var dummy = ex . ToString ( ) ;
209+
210+
211+ // TODO: Log inner exception
218212 }
219213 finally
220214 {
221- if ( args is not null )
222- {
223- //args.Handled = !itIsComplicated;
224- }
215+ args . Handled = true ; // TODO: For test purposes only
216+ //args.Handled = !itIsComplicated; // TODO
225217 }
226218 }
227219
228220 protected async override void OnLaunched ( LaunchActivatedEventArgs args )
229221 {
230222 base . OnLaunched ( args ) ;
231223
232- // TODO: App.GetService<IAppNotificationService>().Show(string.Format("AppNotificationSamplePayload".GetLocalized(), AppContext.BaseDirectory));
224+ //GetService<IAppNotificationService>()?
225+ // .Show(string.Format("AppNotificationSamplePayload".GetLocalized(), AppContext.BaseDirectory));
233226
234- await App . GetService < IActivationService > ( ) . ActivateAsync ( args ) ;
227+ await GetService < IActivationService > ( ) ? . ActivateAsync ( args ) ;
235228 }
236- }
229+ }
0 commit comments