diff --git a/DMS.WPF/App.xaml.cs b/DMS.WPF/App.xaml.cs index 35f3f90..a1a6100 100644 --- a/DMS.WPF/App.xaml.cs +++ b/DMS.WPF/App.xaml.cs @@ -55,7 +55,7 @@ public partial class App : System.Windows.Application protected override async void OnStartup(StartupEventArgs e) { base.OnStartup(e); - ShutdownMode = ShutdownMode.OnExplicitShutdown; + ShutdownMode = ShutdownMode.OnLastWindowClose; ThemeHelper.InitializeTheme(); await Host.StartAsync(); @@ -80,8 +80,8 @@ public partial class App : System.Windows.Application NotificationHelper.ShowError("加载数据时发生错误,如果是连接字符串不正确,可以在设置界面更改:{exception.Message}", exception); } - MainWindow = Host.Services.GetRequiredService(); - MainWindow.Show(); + var splashWindow = Host.Services.GetRequiredService(); + splashWindow.Show(); // 根据配置启动服务 // var connectionSettings = DMS.Config.AppSettings.Load(); diff --git a/DMS.WPF/Services/NavigationService.cs b/DMS.WPF/Services/NavigationService.cs index 3b787e9..cca5932 100644 --- a/DMS.WPF/Services/NavigationService.cs +++ b/DMS.WPF/Services/NavigationService.cs @@ -1,10 +1,13 @@ // 文件: DMS.WPF/Services/NavigationService.cs + using DMS.WPF.ViewModels; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Threading.Tasks; +using System.Windows; using DMS.ViewModels; +using DMS.WPF.Views; namespace DMS.WPF.Services; @@ -50,23 +53,28 @@ public class NavigationService : INavigationService /// public Task ShowMainWindowAsync() { - var mainWindow = _serviceProvider.GetRequiredService(); - mainWindow.Show(); - return Task.CompletedTask; + return App.Current.Dispatcher.InvokeAsync(() => + { + var mainView = _serviceProvider.GetRequiredService(); + // 将 MainView 设置为新的主窗口 + App.Current.MainWindow = mainView; + mainView.Show(); + }) + .Task; } private Type GetViewModelTypeByKey(string key) { return key switch - { - "HomeView" => typeof(HomeViewModel), - "DevicesView" => typeof(DevicesViewModel), - "DeviceDetailView" => typeof(DeviceDetailViewModel), - "VariableTableView" => typeof(VariableTableViewModel), - "MqttsView" => typeof(MqttsViewModel), - "MqttServerDetailView" => typeof(MqttServerDetailViewModel), - "SettingView" => typeof(SettingViewModel), - _ => throw new KeyNotFoundException($"未找到与键 '{key}' 关联的视图模型类型。请检查 NavigationService 的映射配置。") - }; + { + "HomeView" => typeof(HomeViewModel), + "DevicesView" => typeof(DevicesViewModel), + "DeviceDetailView" => typeof(DeviceDetailViewModel), + "VariableTableView" => typeof(VariableTableViewModel), + "MqttsView" => typeof(MqttsViewModel), + "MqttServerDetailView" => typeof(MqttServerDetailViewModel), + "SettingView" => typeof(SettingViewModel), + _ => throw new KeyNotFoundException($"未找到与键 '{key}' 关联的视图模型类型。请检查 NavigationService 的映射配置。") + }; } -} +} \ No newline at end of file diff --git a/DMS.WPF/ViewModels/SplashViewModel.cs b/DMS.WPF/ViewModels/SplashViewModel.cs index ca13178..4e74e13 100644 --- a/DMS.WPF/ViewModels/SplashViewModel.cs +++ b/DMS.WPF/ViewModels/SplashViewModel.cs @@ -29,7 +29,7 @@ public partial class SplashViewModel : ObservableObject /// /// 开始执行初始化任务。 /// - public async Task InitializeAsync() + public async Task InitializeAsync() { try { @@ -50,12 +50,14 @@ public partial class SplashViewModel : ObservableObject // 初始化完成,显示主窗口 var navigationService = (INavigationService)_serviceProvider.GetService(typeof(INavigationService)); await navigationService.ShowMainWindowAsync(); + return true; } catch (Exception ex) { // 处理初始化过程中的异常 LoadingMessage = $"初始化失败: {ex.Message}"; // 在此可以记录日志或显示错误对话框 + return false; } } } diff --git a/DMS.WPF/Views/SplashWindow.xaml.cs b/DMS.WPF/Views/SplashWindow.xaml.cs index bdb7e91..14377cc 100644 --- a/DMS.WPF/Views/SplashWindow.xaml.cs +++ b/DMS.WPF/Views/SplashWindow.xaml.cs @@ -15,8 +15,11 @@ public partial class SplashWindow : Window DataContext = viewModel; Loaded += async (s, e) => { - await viewModel.InitializeAsync(); - Close(); + var success = await viewModel.InitializeAsync(); + if (success) + { + Close(); + } }; } }