2025-07-26 11:20:03 +08:00
|
|
|
|
// 文件: DMS.WPF/Services/NavigationService.cs
|
2025-07-26 11:51:09 +08:00
|
|
|
|
|
2025-08-25 21:26:18 +08:00
|
|
|
|
using DMS.Helper;
|
|
|
|
|
|
using DMS.ViewModels;
|
2025-07-26 11:20:03 +08:00
|
|
|
|
using DMS.WPF.ViewModels;
|
2025-08-25 21:26:18 +08:00
|
|
|
|
using DMS.WPF.ViewModels.Items;
|
|
|
|
|
|
using DMS.WPF.Views;
|
2025-07-26 11:20:03 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
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>
|
2025-07-30 12:09:00 +08:00
|
|
|
|
public async Task NavigateToAsync(MenuItemViewModel menu)
|
2025-07-26 11:20:03 +08:00
|
|
|
|
{
|
2025-07-30 12:09:00 +08:00
|
|
|
|
if (string.IsNullOrEmpty(menu.TargetViewKey))
|
2025-07-26 11:20:03 +08:00
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-26 18:58:52 +08:00
|
|
|
|
var mainViewModel = App.Current.Services.GetRequiredService<MainViewModel>();
|
2025-07-30 12:09:00 +08:00
|
|
|
|
var viewModel = GetViewModelByKey(menu.TargetViewKey);
|
2025-08-25 21:26:18 +08:00
|
|
|
|
if (viewModel == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
NotificationHelper.ShowError($"切换界面失败,没有找到界面:{menu.TargetViewKey}");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-26 11:20:03 +08:00
|
|
|
|
|
|
|
|
|
|
if (viewModel is INavigatable navigatableViewModel)
|
|
|
|
|
|
{
|
2025-07-30 12:09:00 +08:00
|
|
|
|
await navigatableViewModel.OnNavigatedToAsync(menu);
|
2025-07-26 11:20:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mainViewModel.CurrentViewModel = viewModel;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-26 12:19:05 +08:00
|
|
|
|
|
2025-07-26 18:58:52 +08:00
|
|
|
|
private ViewModelBase GetViewModelByKey(string key)
|
2025-07-26 11:20:03 +08:00
|
|
|
|
{
|
2025-07-26 18:58:52 +08:00
|
|
|
|
switch (key)
|
|
|
|
|
|
{
|
|
|
|
|
|
case "HomeView":
|
|
|
|
|
|
return App.Current.Services.GetRequiredService<HomeViewModel>();
|
|
|
|
|
|
case "DevicesView":
|
|
|
|
|
|
return App.Current.Services.GetRequiredService<DevicesViewModel>();
|
|
|
|
|
|
case "DeviceDetailView":
|
|
|
|
|
|
return App.Current.Services.GetRequiredService<DeviceDetailViewModel>();
|
|
|
|
|
|
case "DataTransformView":
|
|
|
|
|
|
return App.Current.Services.GetRequiredService<DataTransformViewModel>();
|
|
|
|
|
|
case "VariableTableView":
|
|
|
|
|
|
return App.Current.Services.GetRequiredService<VariableTableViewModel>();
|
|
|
|
|
|
case "MqttsView":
|
|
|
|
|
|
return App.Current.Services.GetRequiredService<MqttsViewModel>();
|
|
|
|
|
|
case "MqttServerDetailView":
|
|
|
|
|
|
return App.Current.Services.GetRequiredService<MqttServerDetailViewModel>();
|
|
|
|
|
|
case "SettingView":
|
|
|
|
|
|
return App.Current.Services.GetRequiredService<SettingViewModel>();
|
|
|
|
|
|
default:
|
2025-08-25 21:26:18 +08:00
|
|
|
|
return null;
|
2025-07-26 18:58:52 +08:00
|
|
|
|
}
|
2025-07-26 11:20:03 +08:00
|
|
|
|
}
|
2025-07-26 11:51:09 +08:00
|
|
|
|
}
|