Skip to content
This repository was archived by the owner on Mar 6, 2023. It is now read-only.

Commit 815b6fe

Browse files
committed
将 public IServiceProvider Services 移到 Application 中并初始化
1 parent d23401c commit 815b6fe

File tree

11 files changed

+103
-65
lines changed

11 files changed

+103
-65
lines changed

Samples.ClassLibrary1/NotifyIconHelper.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static class NotifyIconHelper
2121
/// </summary>
2222
/// <param name="notifyIcon"></param>
2323
/// <param name="exit"></param>
24-
public static void Init(NotifyIcon notifyIcon, Action exit)
24+
public static void Init(NotifyIcon notifyIcon, Action? exit)
2525
{
2626
notifyIcon.Text = "My Notify Icon";
2727
notifyIcon.Icon = Icon;
@@ -62,10 +62,7 @@ public static void Init(NotifyIcon notifyIcon, Action exit)
6262
notifyIcon.ContextMenuStrip.Items.Add(new()
6363
{
6464
Text = "Exit",
65-
Command = ReactiveCommand.Create(() =>
66-
{
67-
exit();
68-
}),
65+
Command = ReactiveCommand.Create(() => exit?.Invoke()),
6966
});
7067
}
7168
}

Samples.MauiApp1/MauiProgram.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
#if WINDOWS
2+
using NotifyIcon = System.Windows.NotifyIcon;
3+
#endif
4+
15
namespace Samples.MauiApp1
26
{
37
public static partial class MauiProgram
48
{
5-
public static MauiApp CreateMauiApp()
9+
public static MauiApp CreateMauiApplication(this IPlatformApplication application)
610
{
711
var builder = MauiApp.CreateBuilder();
12+
ConfigureServices(builder.Services);
813
builder
914
.UseMauiApp<App>()
1015
.ConfigureFonts(fonts =>
@@ -13,7 +18,32 @@ public static MauiApp CreateMauiApp()
1318
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
1419
});
1520

16-
return builder.Build();
21+
var app = builder.Build();
22+
23+
#if WINDOWS
24+
var notifyIcon = app.Services.GetRequiredService<NotifyIcon>();
25+
NotifyIconHelper.Init(notifyIcon, application.Exit);
26+
#endif
27+
28+
return app;
29+
}
30+
31+
/// <summary>
32+
///
33+
/// </summary>
34+
/// <param name="services"></param>
35+
static void ConfigureServices(IServiceCollection services)
36+
{
37+
#if WINDOWS
38+
services.AddSingleton(typeof(NotifyIcon), NotifyIcon.ImplType);
39+
#endif
40+
}
41+
42+
public interface IPlatformApplication
43+
{
44+
IServiceProvider Services { get; }
45+
46+
void Exit();
1747
}
1848
}
1949
}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
using Android.App;
1+
using Android.App;
22
using Android.Runtime;
33

44
namespace Samples.MauiApp1
55
{
66
[Application]
7-
public class MainApplication : MauiApplication
7+
public class MainApplication : MauiApplication, MauiProgram.IPlatformApplication
88
{
99
public MainApplication(IntPtr handle, JniHandleOwnership ownership)
1010
: base(handle, ownership)
1111
{
1212
}
1313

14-
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
14+
protected override MauiApp CreateMauiApp() => this.CreateMauiApplication();
15+
16+
void MauiProgram.IPlatformApplication.Exit()
17+
{
18+
}
1519
}
1620
}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
using Foundation;
1+
using Foundation;
22

33
namespace Samples.MauiApp1
44
{
55
[Register("AppDelegate")]
6-
public class AppDelegate : MauiUIApplicationDelegate
6+
public class AppDelegate : MauiUIApplicationDelegate, MauiProgram.IPlatformApplication
77
{
8-
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
8+
protected override MauiApp CreateMauiApp() => this.CreateMauiApplication();
9+
10+
void MauiProgram.IPlatformApplication.Exit()
11+
{
12+
}
913
}
1014
}

Samples.MauiApp1/Platforms/Tizen/Main.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Samples.MauiApp1
66
{
7-
class Program : MauiApplication
7+
class Program : MauiApplication, MauiProgram.IPlatformApplication
88
{
99
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
1010

@@ -13,5 +13,9 @@ static void Main(string[] args)
1313
var app = new Program();
1414
app.Run(args);
1515
}
16+
17+
void MauiProgram.IPlatformApplication.Exit()
18+
{
19+
}
1620
}
1721
}

Samples.MauiApp1/Platforms/Windows/App.xaml.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microsoft.UI.Xaml;
1+
using Microsoft.UI.Xaml;
22

33
// To learn more about WinUI, the WinUI project structure,
44
// and more about our project templates, see: http://aka.ms/winui-project-info.
@@ -8,8 +8,10 @@ namespace Samples.MauiApp1.WinUI
88
/// <summary>
99
/// Provides application-specific behavior to supplement the default Application class.
1010
/// </summary>
11-
public partial class App : MauiWinUIApplication
11+
public partial class App : MauiWinUIApplication, MauiProgram.IPlatformApplication
1212
{
13+
readonly Action<IServiceCollection>? configureServices;
14+
1315
/// <summary>
1416
/// Initializes the singleton application object. This is the first line of authored code
1517
/// executed, and as such is the logical equivalent of main() or WinMain().
@@ -19,6 +21,6 @@ public App()
1921
this.InitializeComponent();
2022
}
2123

22-
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
24+
protected override MauiApp CreateMauiApp() => this.CreateMauiApplication();
2325
}
2426
}

Samples.MauiApp1/Platforms/Windows/MauiProgram.cs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace Samples.MauiApp1
99
{
1010
partial class MauiProgram
1111
{
12+
static event Action? Exit;
13+
1214
[DllImport("Microsoft.ui.xaml.dll")]
1315
static extern void XamlCheckProcessRequirements();
1416

@@ -22,33 +24,15 @@ static void Main(string[] args)
2224
var context = new DispatcherQueueSynchronizationContext(DispatcherQueue.GetForCurrentThread());
2325
SynchronizationContext.SetSynchronizationContext(context);
2426

25-
var services = new ServiceCollection();
26-
ConfigureServices(services);
27-
value = services.BuildServiceProvider();
28-
2927
var app = new WinUIApp();
30-
31-
var notifyIcon = value.GetRequiredService<NotifyIcon>();
32-
Exit += (_, _) =>
28+
Exit += () =>
3329
{
30+
var notifyIcon = app.Services.GetRequiredService<NotifyIcon>();
3431
notifyIcon.Dispose();
3532
};
36-
NotifyIconHelper.Init(notifyIcon, app.Exit);
3733
});
3834

39-
Exit?.Invoke(null, EventArgs.Empty);
40-
}
41-
42-
static IServiceProvider? value;
43-
static event EventHandler? Exit;
44-
45-
/// <summary>
46-
///
47-
/// </summary>
48-
/// <param name="services"></param>
49-
static void ConfigureServices(IServiceCollection services)
50-
{
51-
services.AddSingleton(typeof(NotifyIcon), NotifyIcon.ImplType);
35+
Exit?.Invoke();
5236
}
5337
}
5438
}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
using Foundation;
1+
using Foundation;
22

33
namespace Samples.MauiApp1
44
{
55
[Register("AppDelegate")]
6-
public class AppDelegate : MauiUIApplicationDelegate
6+
public class AppDelegate : MauiUIApplicationDelegate, MauiProgram.IPlatformApplication
77
{
8-
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
8+
protected override MauiApp CreateMauiApp() => this.CreateMauiApplication();
9+
10+
void MauiProgram.IPlatformApplication.Exit()
11+
{
12+
}
913
}
1014
}

Samples.WinUI3App1/App.xaml.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.Extensions.DependencyInjection;
12
using Microsoft.UI.Xaml;
23
using Microsoft.UI.Xaml.Controls;
34
using Microsoft.UI.Xaml.Controls.Primitives;
@@ -15,6 +16,7 @@
1516
using Windows.ApplicationModel.Activation;
1617
using Windows.Foundation;
1718
using 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
}

Samples.WinUI3App1/WinUIProgram.cs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace Samples.WinUI3App1
1111
{
1212
static class WinUIProgram
1313
{
14+
static event Action? Exit;
15+
1416
[DllImport("Microsoft.ui.xaml.dll")]
1517
static extern void XamlCheckProcessRequirements();
1618

@@ -24,33 +26,15 @@ static void Main(string[] args)
2426
var context = new DispatcherQueueSynchronizationContext(DispatcherQueue.GetForCurrentThread());
2527
SynchronizationContext.SetSynchronizationContext(context);
2628

27-
var services = new ServiceCollection();
28-
ConfigureServices(services);
29-
value = services.BuildServiceProvider();
30-
3129
var app = new App();
32-
33-
var notifyIcon = value.GetRequiredService<NotifyIcon>();
34-
Exit += (_, _) =>
30+
Exit += () =>
3531
{
32+
var notifyIcon = app.Services.GetRequiredService<NotifyIcon>();
3633
notifyIcon.Dispose();
3734
};
38-
NotifyIconHelper.Init(notifyIcon, app.Exit);
3935
});
4036

41-
Exit?.Invoke(null, EventArgs.Empty);
42-
}
43-
44-
static IServiceProvider? value;
45-
static event EventHandler? Exit;
46-
47-
/// <summary>
48-
///
49-
/// </summary>
50-
/// <param name="services"></param>
51-
static void ConfigureServices(IServiceCollection services)
52-
{
53-
services.AddSingleton(typeof(NotifyIcon), NotifyIcon.ImplType);
37+
Exit?.Invoke();
5438
}
5539
}
5640
}

0 commit comments

Comments
 (0)