2025-07-26 11:20:03 +08:00
|
|
|
|
// 文件: DMS.WPF/Services/NavigationService.cs
|
2025-07-26 11:51:09 +08:00
|
|
|
|
|
2025-07-26 11:20:03 +08:00
|
|
|
|
using DMS.WPF.ViewModels;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2025-07-26 11:51:09 +08:00
|
|
|
|
using System.Windows;
|
2025-07-26 11:20:03 +08:00
|
|
|
|
using DMS.ViewModels;
|
2025-07-26 11:51:09 +08:00
|
|
|
|
using DMS.WPF.Views;
|
2025-07-26 11:20:03 +08:00
|
|
|
|
|
|
|
|
|
|
namespace DMS.WPF.Services;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// INavigationService 的实现,负责解析ViewModel并处理参数传递。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class NavigationService : INavigationService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 构造函数。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public NavigationService(IServiceProvider serviceProvider)
|
|
|
|
|
|
{
|
|
|
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 导航到指定键的视图,并传递参数。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task NavigateToAsync(string viewKey, object parameter = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(viewKey))
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var mainViewModel = _serviceProvider.GetRequiredService<MainViewModel>();
|
|
|
|
|
|
var viewModelType = GetViewModelTypeByKey(viewKey);
|
|
|
|
|
|
var viewModel = _serviceProvider.GetRequiredService(viewModelType) as ViewModelBase;
|
|
|
|
|
|
|
|
|
|
|
|
if (viewModel is INavigatable navigatableViewModel)
|
|
|
|
|
|
{
|
|
|
|
|
|
await navigatableViewModel.OnNavigatedToAsync(parameter);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mainViewModel.CurrentViewModel = viewModel;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 显示主窗口。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Task ShowMainWindowAsync()
|
|
|
|
|
|
{
|
2025-07-26 11:51:09 +08:00
|
|
|
|
return App.Current.Dispatcher.InvokeAsync(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var mainView = _serviceProvider.GetRequiredService<MainView>();
|
|
|
|
|
|
// 将 MainView 设置为新的主窗口
|
|
|
|
|
|
App.Current.MainWindow = mainView;
|
|
|
|
|
|
mainView.Show();
|
|
|
|
|
|
})
|
|
|
|
|
|
.Task;
|
2025-07-26 11:20:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Type GetViewModelTypeByKey(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return key switch
|
2025-07-26 11:51:09 +08:00
|
|
|
|
{
|
|
|
|
|
|
"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 的映射配置。")
|
|
|
|
|
|
};
|
2025-07-26 11:20:03 +08:00
|
|
|
|
}
|
2025-07-26 11:51:09 +08:00
|
|
|
|
}
|