File tree Expand file tree Collapse file tree 4 files changed +31
-17
lines changed
Expand file tree Collapse file tree 4 files changed +31
-17
lines changed Original file line number Diff line number Diff line change 1+ using System ;
12using Avalonia ;
23using Avalonia . Controls . ApplicationLifetimes ;
34using Avalonia . Data . Core ;
45using Avalonia . Data . Core . Plugins ;
56using Avalonia . Markup . Xaml ;
67using LLCOM . ViewModels ;
78using LLCOM . Views ;
9+ using Microsoft . Extensions . DependencyInjection ;
810
911namespace 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
Original file line number Diff line number Diff line change 66
77namespace LLCOM . ViewModels ;
88
9- public partial class MainViewModel : ViewModelBase
9+ public partial class MainViewModel ( Func < Type , ViewModelBase ? > getService ) : ViewModelBase
1010{
1111
1212}
Original file line number Diff line number Diff line change 1- using CommunityToolkit . Mvvm . ComponentModel ;
1+ using System ;
2+ using CommunityToolkit . Mvvm . ComponentModel ;
23using System . Threading . Tasks ;
4+ using Microsoft . Extensions . DependencyInjection ;
35
46namespace 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}
Original file line number Diff line number Diff line change 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 >
You can’t perform that action at this time.
0 commit comments