Skip to content

Commit 54a45d8

Browse files
committed
add: 简单搞个依赖注入,以后有需要再改
1 parent d4a7c18 commit 54a45d8

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

llcomNext/LLCOM/App.axaml.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
using System;
12
using Avalonia;
23
using Avalonia.Controls.ApplicationLifetimes;
34
using Avalonia.Data.Core;
45
using Avalonia.Data.Core.Plugins;
56
using Avalonia.Markup.Xaml;
67
using LLCOM.ViewModels;
78
using LLCOM.Views;
9+
using Microsoft.Extensions.DependencyInjection;
810

911
namespace LLCOM
1012
{
@@ -17,14 +19,25 @@ public override void Initialize()
1719

1820
public override void OnFrameworkInitializationCompleted()
1921
{
22+
var collection = new ServiceCollection();
23+
ServiceProvider? serviceProvider = null;
24+
25+
collection.AddSingleton<MainWindowViewModel>();
26+
collection.AddSingleton<MainViewModel>();
27+
28+
//用于获取别的ViewModel
29+
collection.AddSingleton<Func<Type, ViewModelBase?>>(x => type => serviceProvider!.GetService(type) as ViewModelBase);
30+
31+
serviceProvider = collection.BuildServiceProvider();
32+
2033
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
2134
{
2235
// Line below is needed to remove Avalonia data validation.
2336
// Without this line you will get duplicate validations from both Avalonia and CT
2437
BindingPlugins.DataValidators.RemoveAt(0);
2538
desktop.MainWindow = new MainWindow
2639
{
27-
DataContext = new MainWindowViewModel(),
40+
DataContext = serviceProvider.GetService<MainWindowViewModel>()
2841
};
2942
}
3043

llcomNext/LLCOM/ViewModels/MainViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace LLCOM.ViewModels;
88

9-
public partial class MainViewModel : ViewModelBase
9+
public partial class MainViewModel(Func<Type, ViewModelBase?> getService) : ViewModelBase
1010
{
1111

1212
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
using CommunityToolkit.Mvvm.ComponentModel;
1+
using System;
2+
using CommunityToolkit.Mvvm.ComponentModel;
23
using System.Threading.Tasks;
4+
using Microsoft.Extensions.DependencyInjection;
35

46
namespace LLCOM.ViewModels
57
{
68

7-
public partial class MainWindowViewModel : ViewModelBase
9+
public partial class MainWindowViewModel(Func<Type, ViewModelBase?> getService) : ViewModelBase
810
{
9-
public MainWindowViewModel()
10-
{
11-
12-
}
11+
[ObservableProperty]
12+
private MainViewModel _mainViewModel = (MainViewModel)getService(typeof(MainViewModel))!;
1313
}
1414
}
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
<Window
2+
Height="800"
3+
Icon="/Assets/llcom-logo.ico"
4+
Title="LLCOM Next"
5+
Width="1200"
6+
WindowStartupLocation="CenterScreen"
7+
mc:Ignorable="d"
28
x:Class="LLCOM.Views.MainWindow"
9+
x:DataType="vm:MainWindowViewModel"
310
xmlns="https://github.com/avaloniaui"
4-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
511
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
612
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
713
xmlns:views="clr-namespace:LLCOM.Views"
814
xmlns:vm="using:LLCOM.ViewModels"
9-
Title="LLCOM Next"
10-
Height="800"
11-
Width="1200"
12-
x:DataType="vm:MainWindowViewModel"
13-
Icon="/Assets/llcom-logo.ico"
14-
WindowStartupLocation="CenterScreen"
15-
mc:Ignorable="d">
15+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
1616
<!-- 这里别写别的ui控件,不然跨平台会出问题 -->
17-
<views:MainView />
17+
<!-- 绑定到MainViewModel -->
18+
<UserControl Content="{Binding MainViewModel}" />
1819
</Window>

0 commit comments

Comments
 (0)