Skip to content

Commit 46de64b

Browse files
committed
fix: 修复异步加载导致的第一页初始加载不完全的问题
1 parent 625ab65 commit 46de64b

File tree

6 files changed

+19
-13
lines changed

6 files changed

+19
-13
lines changed

PCL.Neo/App.axaml.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public override void Initialize()
3333

3434
private static IServiceProvider ConfigureServices() => new ServiceCollection()
3535
.AddTransient<MainWindowViewModel>()
36-
.AddTransient<ViewModels.Home.HomeViewModel>()
36+
.AddTransient<HomeViewModel>()
3737
.AddTransient<HomeSubViewModel>()
3838
.AddTransient<VersionManagerViewModel>()
3939
.AddTransient<GameSettingsViewModel>()
@@ -49,7 +49,7 @@ public override void Initialize()
4949
.AddSingleton<IJavaManager, JavaManager>()
5050
.AddSingleton<DownloadService>()
5151
.AddSingleton<GameService>()
52-
.AddSingleton<GameLauncher>(provider => new GameLauncher(provider.GetRequiredService<GameService>()))
52+
.AddSingleton<GameLauncher>()
5353
.AddSingleton<UserService>()
5454
.BuildServiceProvider();
5555

@@ -65,6 +65,8 @@ public override void OnFrameworkInitializationCompleted()
6565
// More info: https://docs.avaloniaui.net/docs/guides/development-guides/data-validation#manage-validationplugins
6666
DisableAvaloniaDataAnnotationValidation();
6767
desktop.MainWindow = new MainWindow { DataContext = vm };
68+
// 由于导航改成了异步方法,在构造函数中无法正常导向首页,需要在此处导向
69+
Ioc.Default.GetRequiredService<INavigationService>().GotoAsync<HomeViewModel>();
6870
}
6971

7072
base.OnFrameworkInitializationCompleted();

PCL.Neo/ViewModels/Home/GameSettingsViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class VersionComponent
4141
public bool IsClickable { get; set; } = false;
4242
}
4343

44-
[SubViewModelOf(typeof(HomeViewModel))]
44+
[SubViewModelOf(typeof(HomeViewModelBackup))]
4545
public partial class GameSettingsViewModel : ViewModelBase
4646
{
4747
private readonly INavigationService _navigationService;

PCL.Neo/ViewModels/Home/HomeViewModel.cs renamed to PCL.Neo/ViewModels/Home/HomeViewModelBackup.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77

88
namespace PCL.Neo.ViewModels.Home
99
{
10-
public partial class HomeViewModel : ObservableObject
10+
// TODO)) 暂未使用
11+
public partial class HomeViewModelBackup : ObservableObject
1112
{
1213
private readonly INavigationService _navigationService;
1314

14-
public HomeViewModel(INavigationService navigationService)
15+
public HomeViewModelBackup(INavigationService navigationService)
1516
{
1617
_navigationService = navigationService;
1718
}
@@ -22,12 +23,12 @@ private async Task ManageUsers()
2223
// 导航到用户管理页面
2324
await _navigationService.GotoAsync<HomeSubViewModel>();
2425
}
25-
26+
2627
[RelayCommand]
2728
private async Task ManageVersions()
2829
{
2930
// 导航到版本管理页面
3031
await _navigationService.GotoAsync<VersionManagerViewModel>();
3132
}
3233
}
33-
}
34+
}

PCL.Neo/ViewModels/Home/VersionManagerViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class VersionItem
3535
public VersionInfo VersionInfo { get; set; } = null!;
3636
}
3737

38-
[SubViewModelOf(typeof(HomeViewModel))]
38+
[SubViewModelOf(typeof(HomeViewModelBackup))]
3939
public partial class VersionManagerViewModel : ViewModelBase
4040
{
4141
private readonly INavigationService _navigationService;

PCL.Neo/ViewModels/MainWindowViewModel.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ private int CheckedBtn
3838
}
3939

4040

41-
[ObservableProperty] private ViewModelBase? _currentViewModel;
41+
[ObservableProperty] [NotifyPropertyChangedFor(nameof(CheckedBtn))]
42+
private ViewModelBase? _currentViewModel;
43+
4244
[ObservableProperty] private ViewModelBase? _currentSubViewModel;
4345

4446
[ObservableProperty] private bool _canGoBack;
@@ -68,9 +70,10 @@ public MainWindowViewModel(INavigationService navigationService)
6870
NavigationService.CurrentViewModelChanged += vm =>
6971
{
7072
CurrentViewModel = vm;
71-
7273
// 更新返回按钮状态
7374
CanGoBack = NavigationService.CanGoBack;
75+
// 由外部的页面跳转反向触发设置按钮状态
76+
UpdateNavBtnState();
7477
};
7578
NavigationService.CurrentSubViewModelChanged += vm => CurrentSubViewModel = vm;
7679
}
@@ -144,8 +147,9 @@ private void UpdateNavBtnState()
144147
{
145148
HomeViewModel => 1,
146149
DownloadViewModel => 2,
147-
SetupViewModel => 3,
148-
LogViewModel => 4,
150+
// LinkViewModel => 3,
151+
SetupViewModel => 4,
152+
// MoreViewModel => 4,
149153
_ => throw new ArgumentOutOfRangeException()
150154
};
151155
}

PCL.Neo/Views/Home/HomeSubView.axaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
xmlns:home="clr-namespace:PCL.Neo.ViewModels.Home"
1010
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
1111
xmlns:pc="using:PCL.Neo.Controls"
12-
xmlns:vm="using:PCL.Neo.ViewModels"
1312
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
1413
<ScrollViewer>
1514
<StackPanel Orientation="Vertical">

0 commit comments

Comments
 (0)