Skip to content

Commit a96548c

Browse files
Merge pull request #14 from nullinside-development-group/feat/di
feat: Convert new version window to DI
2 parents 7ad50ca + 2244a8e commit a96548c

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/TwitchStreamingTools/App.axaml.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public override void OnFrameworkInitializationCompleted() {
4343
var vm = services.GetRequiredService<MainWindowViewModel>();
4444
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) {
4545
desktop.MainWindow = new MainWindow {
46-
DataContext = vm
46+
DataContext = vm,
47+
ServiceProvider = services
4748
};
4849
}
4950

src/TwitchStreamingTools/Services/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public static void AddCommonServices(this IServiceCollection collection) {
2828
collection.AddTransient<MainWindowViewModel>();
2929
collection.AddTransient<AccountViewModel>();
3030
collection.AddTransient<ChatViewModel>();
31+
collection.AddTransient<NewVersionWindowViewModel>();
3132
collection.AddTransient<ITwitchApiProxy, TwitchApiWrapper>(TwitchApiWrapperFactory);
3233
}
3334

src/TwitchStreamingTools/Views/MainWindow.axaml.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using Avalonia.Controls;
77
using Avalonia.Threading;
88

9+
using Microsoft.Extensions.DependencyInjection;
10+
911
using Nullinside.Api.Common.Desktop;
1012
#if !DEBUG
1113
using TwitchStreamingTools.ViewModels;
@@ -17,6 +19,11 @@ namespace TwitchStreamingTools.Views;
1719
/// The main application window.
1820
/// </summary>
1921
public partial class MainWindow : Window {
22+
/// <summary>
23+
/// The service provider for DI.
24+
/// </summary>
25+
public IServiceProvider? ServiceProvider { get; set; }
26+
2027
/// <summary>
2128
/// Initializes a new instance of the <see cref="MainWindow" /> class.
2229
/// </summary>
@@ -53,11 +60,15 @@ protected override void OnInitialized() {
5360
}
5461

5562
#if !DEBUG
63+
var vm = ServiceProvider?.GetRequiredService<NewVersionWindowViewModel>();
64+
if (null == vm) {
65+
return;
66+
}
67+
68+
vm.LocalVersion = localVersion;
5669
Dispatcher.UIThread.Post(async () => {
5770
var versionWindow = new NewVersionWindow {
58-
DataContext = new NewVersionWindowViewModel {
59-
LocalVersion = localVersion
60-
}
71+
DataContext = vm
6172
};
6273

6374
await versionWindow.ShowDialog(this);

0 commit comments

Comments
 (0)