Skip to content

Commit f9983b5

Browse files
committed
Move dependency injection codes to constructor
1 parent 8b290a6 commit f9983b5

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

Flow.Launcher/App.xaml.cs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,31 @@ public partial class App : IDisposable, ISingleInstanceApp
3030
public static IPublicAPI API { get; private set; }
3131
private const string Unique = "Flow.Launcher_Unique_Application_Mutex";
3232
private static bool _disposed;
33-
private Settings _settings;
33+
private readonly Settings _settings;
34+
35+
public App()
36+
{
37+
// Initialize settings
38+
var storage = new FlowLauncherJsonStorage<Settings>();
39+
_settings = storage.Load();
40+
_settings.Initialize(storage);
41+
_settings.WMPInstalled = WindowsMediaPlayerHelper.IsWindowsMediaPlayerInstalled();
42+
43+
// Configure the dependency injection container
44+
var host = Host.CreateDefaultBuilder()
45+
.UseContentRoot(AppContext.BaseDirectory)
46+
.ConfigureServices(services => services
47+
.AddSingleton(_ => _settings)
48+
.AddSingleton<Updater>()
49+
.AddSingleton<Portable>()
50+
.AddSingleton<SettingWindowViewModel>()
51+
.AddSingleton<IAlphabet, PinyinAlphabet>()
52+
.AddSingleton<StringMatcher>()
53+
.AddSingleton<IPublicAPI, PublicAPIInstance>()
54+
.AddSingleton<MainViewModel>()
55+
).Build();
56+
Ioc.Default.ConfigureServices(host.Services);
57+
}
3458

3559
[STAThread]
3660
public static void Main()
@@ -49,27 +73,6 @@ private async void OnStartupAsync(object sender, StartupEventArgs e)
4973
{
5074
await Stopwatch.NormalAsync("|App.OnStartup|Startup cost", async () =>
5175
{
52-
// Initialize settings
53-
var storage = new FlowLauncherJsonStorage<Settings>();
54-
_settings = storage.Load();
55-
_settings.Initialize(storage);
56-
_settings.WMPInstalled = WindowsMediaPlayerHelper.IsWindowsMediaPlayerInstalled();
57-
58-
// Configure the dependency injection container
59-
var host = Host.CreateDefaultBuilder()
60-
.UseContentRoot(AppContext.BaseDirectory)
61-
.ConfigureServices(services => services
62-
.AddSingleton(_ => _settings)
63-
.AddSingleton<Updater>()
64-
.AddSingleton<Portable>()
65-
.AddSingleton<SettingWindowViewModel>()
66-
.AddSingleton<IAlphabet, PinyinAlphabet>()
67-
.AddSingleton<StringMatcher>()
68-
.AddSingleton<IPublicAPI, PublicAPIInstance>()
69-
.AddSingleton<MainViewModel>()
70-
).Build();
71-
Ioc.Default.ConfigureServices(host.Services);
72-
7376
API = Ioc.Default.GetRequiredService<IPublicAPI>();
7477

7578
Ioc.Default.GetRequiredService<Updater>().Initialize(Launcher.Properties.Settings.Default.GithubRepo);

0 commit comments

Comments
 (0)