@@ -24,105 +24,107 @@ namespace Wpf.Ui.Demo;
2424/// </summary>
2525public partial class App
2626{
27- /// <summary>
28- /// A program abstraction.
29- /// </summary>
30- private IHost _host ;
27+ // The .NET Generic Host provides dependency injection, configuration, logging, and other services.
28+ // https://docs.microsoft.com/dotnet/core/extensions/generic-host
29+ // https://docs.microsoft.com/dotnet/core/extensions/dependency-injection
30+ // https://docs.microsoft.com/dotnet/core/extensions/configuration
31+ // https://docs.microsoft.com/dotnet/core/extensions/logging
32+ private static readonly IHost _host = Host
33+ . CreateDefaultBuilder ( )
34+ . ConfigureAppConfiguration ( c => { c . SetBasePath ( Path . GetDirectoryName ( Assembly . GetEntryAssembly ( ) ! . Location ) ) ; } )
35+ . ConfigureServices ( ( context , services ) =>
36+ {
37+ // App Host
38+ services . AddHostedService < ApplicationHostService > ( ) ;
3139
32- /// <summary>
33- /// Occurs when the application is loading.
34- /// </summary>
35- private async void OnStartup ( object sender , StartupEventArgs e )
36- {
37- // For more information about .NET generic host see https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-6.0
38- _host = Host . CreateDefaultBuilder ( e . Args )
39- . ConfigureAppConfiguration ( c =>
40- {
41- c . SetBasePath ( Path . GetDirectoryName ( Assembly . GetEntryAssembly ( ) ! . Location ) ) ;
42- } )
43- . ConfigureServices ( ConfigureServices )
44- . Build ( ) ;
40+ // Theme manipulation
41+ services . AddSingleton < IThemeService , ThemeService > ( ) ;
4542
46- await _host . StartAsync ( ) ;
47- }
43+ // Taskbar manipulation
44+ services . AddSingleton < ITaskBarService , TaskBarService > ( ) ;
4845
49- /// <summary>
50- /// Configures the services for the application.
51- /// </summary>
52- private static void ConfigureServices ( HostBuilderContext context , IServiceCollection services )
53- {
54- // App Host
55- services . AddHostedService < ApplicationHostService > ( ) ;
46+ // Snackbar service
47+ services . AddSingleton < ISnackbarService , SnackbarService > ( ) ;
5648
57- // Theme manipulation
58- services . AddSingleton < IThemeService , ThemeService > ( ) ;
49+ // Dialog service
50+ services . AddSingleton < IDialogService , DialogService > ( ) ;
5951
60- // Taskbar manipulation
61- services . AddSingleton < ITaskBarService , TaskBarService > ( ) ;
52+ // Tray icon
53+ // Just in case you wondering, it does not work yet
54+ // !! Experimental
55+ services . AddSingleton < INotifyIconService , NotifyIconService > ( ) ;
6256
63- //Snackbar service
64- services . AddSingleton < ISnackbarService , SnackbarService > ( ) ;
57+ // Page resolver service
58+ services . AddSingleton < IPageService , PageService > ( ) ;
6559
66- //Dialog service
67- services . AddSingleton < IDialogService , DialogService > ( ) ;
60+ // Page resolver service
61+ services . AddSingleton < ITestWindowService , TestWindowService > ( ) ;
6862
69- // Tray icon
70- // Just in case you wondering, it does not work yet
71- // !! Experimental
72- services . AddSingleton < INotifyIconService , NotifyIconService > ( ) ;
63+ // Service containing navigation, same as INavigationWindow... but without window
64+ services . AddSingleton < INavigationService , NavigationService > ( ) ;
7365
74- // Page resolver service
75- services . AddSingleton < IPageService , PageService > ( ) ;
66+ // Main window container with navigation
67+ services . AddScoped < INavigationWindow , Views . Container > ( ) ;
68+ services . AddScoped < ContainerViewModel > ( ) ;
7669
77- // Page resolver service
78- services . AddSingleton < ITestWindowService , TestWindowService > ( ) ;
70+ // Views and ViewModels
71+ services . AddScoped < Views . Pages . Dashboard > ( ) ;
7972
80- // Service containing navigation, same as INavigationWindow... but without window
81- services . AddSingleton < INavigationService , NavigationService > ( ) ;
73+ services . AddScoped < Views . Pages . ExperimentalDashboard > ( ) ;
74+ services . AddScoped < ExperimentalViewModel > ( ) ;
8275
83- // Main window container with navigation
84- services . AddScoped < INavigationWindow , Views . Container > ( ) ;
85- services . AddScoped < ContainerViewModel > ( ) ;
76+ services . AddScoped < Views . Pages . Controls > ( ) ;
8677
87- // Views and ViewModels
88- services . AddScoped < Views . Pages . Dashboard > ( ) ;
78+ services . AddScoped < Views . Pages . Menus > ( ) ;
8979
90- services . AddScoped < Views . Pages . ExperimentalDashboard > ( ) ;
91- services . AddScoped < ExperimentalViewModel > ( ) ;
80+ services . AddScoped < Views . Pages . Colors > ( ) ;
81+ services . AddScoped < ColorsViewModel > ( ) ;
9282
93- services . AddScoped < Views . Pages . Controls > ( ) ;
83+ services . AddScoped < Views . Pages . Debug > ( ) ;
84+ services . AddScoped < DebugViewModel > ( ) ;
9485
95- services . AddScoped < Views . Pages . Menus > ( ) ;
86+ services . AddScoped < Views . Pages . Buttons > ( ) ;
87+ services . AddScoped < ButtonsViewModel > ( ) ;
9688
97- services . AddScoped < Views . Pages . Colors > ( ) ;
98- services . AddScoped < ColorsViewModel > ( ) ;
89+ services . AddScoped < Views . Pages . Data > ( ) ;
90+ services . AddScoped < DataViewModel > ( ) ;
9991
100- services . AddScoped < Views . Pages . Debug > ( ) ;
101- services . AddScoped < DebugViewModel > ( ) ;
92+ services . AddScoped < Views . Pages . Input > ( ) ;
93+ services . AddScoped < InputViewModel > ( ) ;
10294
103- services . AddScoped < Views . Pages . Buttons > ( ) ;
104- services . AddScoped < ButtonsViewModel > ( ) ;
95+ services . AddScoped < Views . Pages . Icons > ( ) ;
96+ services . AddScoped < IconsViewModel > ( ) ;
10597
106- services . AddScoped < Views . Pages . Data > ( ) ;
107- services . AddScoped < DataViewModel > ( ) ;
98+ // Test windows
99+ services . AddTransient < Views . Windows . TaskManagerWindow > ( ) ;
100+ services . AddTransient < TaskManagerViewModel > ( ) ;
108101
109- services . AddScoped < Views . Pages . Input > ( ) ;
110- services . AddScoped < InputViewModel > ( ) ;
102+ services . AddTransient < Views . Windows . EditorWindow > ( ) ;
103+ services . AddTransient < Views . Windows . SettingsWindow > ( ) ;
104+ services . AddTransient < Views . Windows . StoreWindow > ( ) ;
105+ services . AddTransient < Views . Windows . ExperimentalWindow > ( ) ;
111106
112- services . AddScoped < Views . Pages . Icons > ( ) ;
113- services . AddScoped < IconsViewModel > ( ) ;
107+ // Configuration
108+ services . Configure < AppConfig > ( context . Configuration . GetSection ( nameof ( AppConfig ) ) ) ;
109+ } ) . Build ( ) ;
114110
115- // Test windows
116- services . AddTransient < Views . Windows . TaskManagerWindow > ( ) ;
117- services . AddTransient < TaskManagerViewModel > ( ) ;
118-
119- services . AddTransient < Views . Windows . EditorWindow > ( ) ;
120- services . AddTransient < Views . Windows . SettingsWindow > ( ) ;
121- services . AddTransient < Views . Windows . StoreWindow > ( ) ;
122- services . AddTransient < Views . Windows . ExperimentalWindow > ( ) ;
111+ /// <summary>
112+ /// Gets registered service.
113+ /// </summary>
114+ /// <typeparam name="T">Type of the service to get.</typeparam>
115+ /// <returns>Instance of the service or <see langword="null"/>.</returns>
116+ public static T GetService < T > ( )
117+ where T : class
118+ {
119+ return _host . Services . GetService ( typeof ( T ) ) as T ;
120+ }
123121
124- // Configuration
125- services . Configure < AppConfig > ( context . Configuration . GetSection ( nameof ( AppConfig ) ) ) ;
122+ /// <summary>
123+ /// Occurs when the application is loading.
124+ /// </summary>
125+ private async void OnStartup ( object sender , StartupEventArgs e )
126+ {
127+ await _host . StartAsync ( ) ;
126128 }
127129
128130 /// <summary>
@@ -133,7 +135,6 @@ private async void OnExit(object sender, ExitEventArgs e)
133135 await _host . StopAsync ( ) ;
134136
135137 _host . Dispose ( ) ;
136- _host = null ;
137138 }
138139
139140 /// <summary>
0 commit comments