Vereinsmeisterschaften  22aa7800eae54b428d40e835886cefe1fdefdfdf
This is a software that can be used to manage the internal competition of the swimming club Illertissen called "Vereinsmeisterschaften".
Loading...
Searching...
No Matches
ApplicationHostService.cs
1using Microsoft.Extensions.Hosting;
2
7
9
11public class ApplicationHostService : IHostedService
12{
13 private readonly IServiceProvider _serviceProvider;
14 private readonly INavigationService _navigationService;
15 private readonly IPersistAndRestoreService _persistAndRestoreService;
16 private readonly IThemeSelectorService _themeSelectorService;
17 private readonly IEnumerable<IActivationHandler> _activationHandlers;
18 private IShellWindow _shellWindow;
19 private bool _isInitialized;
20
29 public ApplicationHostService(IServiceProvider serviceProvider, IEnumerable<IActivationHandler> activationHandlers, INavigationService navigationService, IThemeSelectorService themeSelectorService, IPersistAndRestoreService persistAndRestoreService)
30 {
31 _serviceProvider = serviceProvider;
32 _activationHandlers = activationHandlers;
33 _navigationService = navigationService;
34 _themeSelectorService = themeSelectorService;
35 _persistAndRestoreService = persistAndRestoreService;
36 }
37
43 public async Task StartAsync(CancellationToken cancellationToken)
44 {
45 // Initialize services that you need before app activation
46 await InitializeAsync();
47
48 await HandleActivationAsync();
49
50 // Tasks after activation
51 await StartupAsync();
52 _isInitialized = true;
53 }
54
60 public async Task StopAsync(CancellationToken cancellationToken)
61 {
62 _persistAndRestoreService.PersistData();
63 await Task.CompletedTask;
64 }
65
66 private async Task InitializeAsync()
67 {
68 if (!_isInitialized)
69 {
70 _persistAndRestoreService.RestoreData();
71 _themeSelectorService.InitializeTheme();
72 await Task.CompletedTask;
73 }
74 }
75
76 private async Task StartupAsync()
77 {
78 if (!_isInitialized)
79 {
80 await Task.CompletedTask;
81 }
82 }
83
84 private async Task HandleActivationAsync()
85 {
86 var activationHandler = _activationHandlers.FirstOrDefault(h => h.CanHandle());
87
88 if (activationHandler != null)
89 {
90 await activationHandler.HandleAsync();
91 }
92
93 await Task.CompletedTask;
94
95 if (App.Current.Windows.OfType<IShellWindow>().Count() == 0)
96 {
97 // Default activation that navigates to the apps default page
98 _shellWindow = _serviceProvider.GetService(typeof(IShellWindow)) as IShellWindow;
99 _navigationService.Initialize(_shellWindow.GetNavigationFrame());
100 _shellWindow.ShowWindow();
101 _navigationService.NavigateTo<MainViewModel>();
102 await Task.CompletedTask;
103 }
104 }
105}
async Task StopAsync(CancellationToken cancellationToken)
Triggered when the application host is performing a graceful shutdown.
ApplicationHostService(IServiceProvider serviceProvider, IEnumerable< IActivationHandler > activationHandlers, INavigationService navigationService, IThemeSelectorService themeSelectorService, IPersistAndRestoreService persistAndRestoreService)
Constructor for the ApplicationHostService.
async Task StartAsync(CancellationToken cancellationToken)
Triggered when the application host is ready to start the service.
Interface for a service for handling navigation within the application.
Interface for a service to persist and restore application data.
Interface for a service to manage the application theme selection.
Interface for the main shell window of the application.