1+ using Microsoft . Extensions . DependencyInjection ;
12using Microsoft . UI . Xaml ;
23using Microsoft . UI . Xaml . Controls ;
34using Microsoft . UI . Xaml . Controls . Primitives ;
1516using Windows . ApplicationModel . Activation ;
1617using Windows . Foundation ;
1718using Windows . Foundation . Collections ;
19+ using NotifyIcon = System . Windows . NotifyIcon ;
1820
1921// To learn more about WinUI, the WinUI project structure,
2022// and more about our project templates, see: http://aka.ms/winui-project-info.
@@ -26,6 +28,11 @@ namespace Samples.WinUI3App1
2628 /// </summary>
2729 public partial class App : Application
2830 {
31+ readonly Action < IServiceCollection > ? configureServices ;
32+ IServiceProvider ? services ;
33+
34+ public IServiceProvider Services => services ?? throw new ArgumentNullException ( nameof ( services ) ) ;
35+
2936 /// <summary>
3037 /// Initializes the singleton application object. This is the first line of authored code
3138 /// executed, and as such is the logical equivalent of main() or WinMain().
@@ -42,10 +49,26 @@ public App()
4249 /// <param name="args">Details about the launch request and process.</param>
4350 protected override void OnLaunched ( Microsoft . UI . Xaml . LaunchActivatedEventArgs args )
4451 {
52+ var services = new ServiceCollection ( ) ;
53+ ConfigureServices ( services ) ;
54+ this . services = services . BuildServiceProvider ( ) ;
55+
56+ var notifyIcon = Services . GetRequiredService < NotifyIcon > ( ) ;
57+ NotifyIconHelper . Init ( notifyIcon , Exit ) ;
58+
4559 m_window = new MainWindow ( ) ;
4660 m_window . Activate ( ) ;
4761 }
4862
4963 private Window ? m_window ;
64+
65+ /// <summary>
66+ ///
67+ /// </summary>
68+ /// <param name="services"></param>
69+ static void ConfigureServices ( IServiceCollection services )
70+ {
71+ services . AddSingleton ( typeof ( NotifyIcon ) , NotifyIcon . ImplType ) ;
72+ }
5073 }
5174}
0 commit comments