2025-10-03 22:28:58 +08:00
|
|
|
|
using DMS.Core.Models;
|
2025-09-04 19:59:35 +08:00
|
|
|
|
using DMS.WPF.Interfaces;
|
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;
|
2025-10-03 22:28:58 +08:00
|
|
|
|
using DMS.WPF.Views;
|
2025-07-26 11:20:03 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2025-10-03 22:28:58 +08:00
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
2025-07-26 11:20:03 +08:00
|
|
|
|
|
|
|
|
|
|
namespace DMS.WPF.Services;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// INavigationService 的实现,负责解析ViewModel并处理参数传递。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class NavigationService : INavigationService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
2025-09-07 19:48:48 +08:00
|
|
|
|
private readonly INotificationService _notificationService;
|
2025-07-26 11:20:03 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 构造函数。
|
|
|
|
|
|
/// </summary>
|
2025-09-07 19:48:48 +08:00
|
|
|
|
public NavigationService(IServiceProvider serviceProvider,INotificationService notificationService)
|
2025-07-26 11:20:03 +08:00
|
|
|
|
{
|
|
|
|
|
|
_serviceProvider = serviceProvider;
|
2025-09-07 19:48:48 +08:00
|
|
|
|
_notificationService = notificationService;
|
2025-07-26 11:20:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 22:28:58 +08:00
|
|
|
|
|
2025-07-26 11:20:03 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 导航到指定键的视图,并传递参数。
|
|
|
|
|
|
/// </summary>
|
2025-10-03 22:28:58 +08:00
|
|
|
|
public async Task NavigateToAsync(object sender,NavigationParameter parameter)
|
2025-07-26 11:20:03 +08:00
|
|
|
|
{
|
2025-10-03 22:28:58 +08:00
|
|
|
|
if (parameter == null || string.IsNullOrWhiteSpace(parameter.TargetViewKey) )return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var viewModel = GetViewModelByKey(parameter.TargetViewKey);
|
2025-08-25 21:26:18 +08:00
|
|
|
|
if (viewModel == null)
|
|
|
|
|
|
{
|
2025-10-03 22:28:58 +08:00
|
|
|
|
_notificationService.ShowError($"切换界面失败,没有找到界面:{parameter.TargetViewKey}");
|
2025-08-25 21:26:18 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-10-03 22:28:58 +08:00
|
|
|
|
|
|
|
|
|
|
if (sender is INavigatable fromViewModel)
|
2025-07-26 11:20:03 +08:00
|
|
|
|
{
|
2025-10-03 22:28:58 +08:00
|
|
|
|
await fromViewModel.OnNavigatedFromAsync(parameter);
|
2025-07-26 11:20:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-11 11:04:07 +08:00
|
|
|
|
var mainViewModel = App.Current.Services.GetRequiredService<MainViewModel>();
|
2025-10-03 22:28:58 +08:00
|
|
|
|
mainViewModel.CurrentViewModel = viewModel;
|
|
|
|
|
|
|
|
|
|
|
|
if (viewModel is INavigatable toViewModel)
|
2025-09-11 11:04:07 +08:00
|
|
|
|
{
|
2025-10-03 22:28:58 +08:00
|
|
|
|
await toViewModel.OnNavigatedToAsync(parameter);
|
2025-09-11 11:04:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-26 11:20:03 +08:00
|
|
|
|
|
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-09-07 19:48:48 +08:00
|
|
|
|
try
|
2025-07-26 18:58:52 +08:00
|
|
|
|
{
|
2025-09-07 19:48:48 +08:00
|
|
|
|
switch (key)
|
|
|
|
|
|
{
|
2025-10-03 22:28:58 +08:00
|
|
|
|
case nameof(HomeViewModel):
|
2025-09-07 19:48:48 +08:00
|
|
|
|
return App.Current.Services.GetRequiredService<HomeViewModel>();
|
2025-10-03 22:28:58 +08:00
|
|
|
|
case nameof(DevicesViewModel):
|
2025-09-07 19:48:48 +08:00
|
|
|
|
return App.Current.Services.GetRequiredService<DevicesViewModel>();
|
2025-10-03 22:28:58 +08:00
|
|
|
|
case nameof(DeviceDetailViewModel):
|
2025-09-07 19:48:48 +08:00
|
|
|
|
return App.Current.Services.GetRequiredService<DeviceDetailViewModel>();
|
2025-10-03 22:28:58 +08:00
|
|
|
|
case nameof(DataTransformViewModel):
|
2025-09-07 19:48:48 +08:00
|
|
|
|
return App.Current.Services.GetRequiredService<DataTransformViewModel>();
|
2025-10-03 22:28:58 +08:00
|
|
|
|
case nameof(VariableTableViewModel):
|
2025-09-07 19:48:48 +08:00
|
|
|
|
return App.Current.Services.GetRequiredService<VariableTableViewModel>();
|
2025-10-03 22:28:58 +08:00
|
|
|
|
case nameof(VariableHistoryViewModel):
|
2025-09-11 11:04:07 +08:00
|
|
|
|
return App.Current.Services.GetRequiredService<VariableHistoryViewModel>();
|
2025-10-03 22:28:58 +08:00
|
|
|
|
case nameof(LogHistoryViewModel):
|
2025-09-07 19:48:48 +08:00
|
|
|
|
return App.Current.Services.GetRequiredService<LogHistoryViewModel>();
|
2025-10-03 22:28:58 +08:00
|
|
|
|
case nameof(MqttsViewModel):
|
2025-09-07 19:48:48 +08:00
|
|
|
|
return App.Current.Services.GetRequiredService<MqttsViewModel>();
|
2025-10-03 22:28:58 +08:00
|
|
|
|
case nameof(MqttServerDetailViewModel):
|
2025-09-07 19:48:48 +08:00
|
|
|
|
return App.Current.Services.GetRequiredService<MqttServerDetailViewModel>();
|
2025-10-03 22:28:58 +08:00
|
|
|
|
case nameof(SettingViewModel):
|
2025-09-07 19:48:48 +08:00
|
|
|
|
return App.Current.Services.GetRequiredService<SettingViewModel>();
|
2025-10-03 22:28:58 +08:00
|
|
|
|
case nameof(EmailManagementViewModel):
|
2025-09-13 19:08:43 +08:00
|
|
|
|
return App.Current.Services.GetRequiredService<EmailManagementViewModel>();
|
2025-10-03 22:28:58 +08:00
|
|
|
|
case nameof(TriggersViewModel):
|
2025-09-14 19:13:40 +08:00
|
|
|
|
return App.Current.Services.GetRequiredService<TriggersViewModel>();
|
2025-09-07 19:48:48 +08:00
|
|
|
|
default:
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
_notificationService.ShowError($"切换界面失败,获取:{key}对应的ViewModel时发生了错误:{e.Message}");
|
|
|
|
|
|
throw;
|
2025-07-26 18:58:52 +08:00
|
|
|
|
}
|
2025-07-26 11:20:03 +08:00
|
|
|
|
}
|
2025-07-26 11:51:09 +08:00
|
|
|
|
}
|