临时提交

This commit is contained in:
2025-07-26 11:51:09 +08:00
parent db419edde3
commit 2ee17c2560
4 changed files with 33 additions and 20 deletions

View File

@@ -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<SplashWindow>();
MainWindow.Show();
var splashWindow = Host.Services.GetRequiredService<SplashWindow>();
splashWindow.Show();
// 根据配置启动服务
// var connectionSettings = DMS.Config.AppSettings.Load();

View File

@@ -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
/// </summary>
public Task ShowMainWindowAsync()
{
var mainWindow = _serviceProvider.GetRequiredService<MainWindow>();
mainWindow.Show();
return Task.CompletedTask;
return App.Current.Dispatcher.InvokeAsync(() =>
{
var mainView = _serviceProvider.GetRequiredService<MainView>();
// 将 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 的映射配置。")
};
}
}

View File

@@ -29,7 +29,7 @@ public partial class SplashViewModel : ObservableObject
/// <summary>
/// 开始执行初始化任务。
/// </summary>
public async Task InitializeAsync()
public async Task<bool> 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;
}
}
}

View File

@@ -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();
}
};
}
}