完成页面导航功能

This commit is contained in:
2025-07-26 18:58:52 +08:00
parent eedc9f9c7b
commit e509b7de0b
8 changed files with 53 additions and 36 deletions

View File

@@ -161,6 +161,7 @@ public partial class App : System.Windows.Application
services.AddSingleton<IVariableTableAppService,VariableTableAppService>(); services.AddSingleton<IVariableTableAppService,VariableTableAppService>();
services.AddSingleton<INavigationService, NavigationService>(); services.AddSingleton<INavigationService, NavigationService>();
services.AddSingleton<IMenuService, MenuService>(); services.AddSingleton<IMenuService, MenuService>();
services.AddSingleton<IDialogService, DialogService>();
//注册WPF中的服务 //注册WPF中的服务
services.AddSingleton<DataServices>(); services.AddSingleton<DataServices>();
// 注册视图模型 // 注册视图模型
@@ -174,7 +175,7 @@ public partial class App : System.Windows.Application
services.AddSingleton<VariableTableViewModel>(); services.AddSingleton<VariableTableViewModel>();
//services.AddScoped<MqttServerDetailViewModel>(); //services.AddScoped<MqttServerDetailViewModel>();
services.AddSingleton<DeviceDetailViewModel>(); services.AddSingleton<DeviceDetailViewModel>();
services.AddScoped<MqttsViewModel>(); services.AddSingleton<MqttsViewModel>();
//注册View视图 //注册View视图
services.AddSingleton<SplashWindow>(); services.AddSingleton<SplashWindow>();
services.AddSingleton<SettingView>(); services.AddSingleton<SettingView>();

View File

@@ -8,7 +8,7 @@ using iNKORE.UI.WPF.Modern.Controls;
namespace DMS.WPF.Services; namespace DMS.WPF.Services;
public class DialogService public class DialogService :IDialogService
{ {
// private readonly DataServices _dataServices; // private readonly DataServices _dataServices;
// //

View File

@@ -36,9 +36,8 @@ public class NavigationService : INavigationService
return; return;
} }
var mainViewModel = _serviceProvider.GetRequiredService<MainViewModel>(); var mainViewModel = App.Current.Services.GetRequiredService<MainViewModel>();
var viewModelType = GetViewModelTypeByKey(viewKey); var viewModel = GetViewModelByKey(viewKey);
var viewModel = _serviceProvider.GetRequiredService(viewModelType) as ViewModelBase;
if (viewModel is INavigatable navigatableViewModel) if (viewModel is INavigatable navigatableViewModel)
{ {
@@ -49,19 +48,28 @@ public class NavigationService : INavigationService
} }
private ViewModelBase GetViewModelByKey(string key)
private Type GetViewModelTypeByKey(string key)
{ {
return key switch switch (key)
{ {
"HomeView" => typeof(HomeViewModel), case "HomeView":
"DevicesView" => typeof(DevicesViewModel), return App.Current.Services.GetRequiredService<HomeViewModel>();
"DeviceDetailView" => typeof(DeviceDetailViewModel), case "DevicesView":
"VariableTableView" => typeof(VariableTableViewModel), return App.Current.Services.GetRequiredService<DevicesViewModel>();
"MqttsView" => typeof(MqttsViewModel), case "DeviceDetailView":
"MqttServerDetailView" => typeof(MqttServerDetailViewModel), return App.Current.Services.GetRequiredService<DeviceDetailViewModel>();
"SettingView" => typeof(SettingViewModel), case "DataTransformView":
_ => throw new KeyNotFoundException($"未找到与键 '{key}' 关联的视图模型类型。请检查 NavigationService 的映射配置。") 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:
throw new KeyNotFoundException($"未找到与键 '{key}' 关联的视图模型类型。请检查 NavigationService 的映射配置。");
}
} }
} }

View File

@@ -17,7 +17,7 @@ namespace DMS.WPF.ViewModels;
/// <summary> /// <summary>
/// 设备管理视图模型,负责设备的增删改查操作。 /// 设备管理视图模型,负责设备的增删改查操作。
/// </summary> /// </summary>
public partial class DevicesViewModel : ViewModelBase public partial class DevicesViewModel : ViewModelBase,INavigatable
{ {
private readonly DataServices _dataServices; private readonly DataServices _dataServices;
private readonly IDialogService _dialogService; private readonly IDialogService _dialogService;
@@ -197,4 +197,9 @@ public partial class DevicesViewModel : ViewModelBase
deviceDetailVm.CurrentDevice = SelectedDevice; deviceDetailVm.CurrentDevice = SelectedDevice;
MessageHelper.SendNavgatorMessage(deviceDetailVm); MessageHelper.SendNavgatorMessage(deviceDetailVm);
} }
public async Task OnNavigatedToAsync(object parameter)
{
}
} }

View File

@@ -11,6 +11,7 @@ using DMS.Helper;
using DMS.Services; using DMS.Services;
using DMS.ViewModels; using DMS.ViewModels;
using DMS.WPF.Helper; using DMS.WPF.Helper;
using DMS.WPF.ViewModels.Items;
using iNKORE.UI.WPF.Modern.Common.IconKeys; using iNKORE.UI.WPF.Modern.Common.IconKeys;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@@ -27,13 +28,14 @@ public partial class MainViewModel : ViewModelBase
{ {
public DataServices DataServices { get; } public DataServices DataServices { get; }
private readonly IDialogService _dialogService; private readonly IDialogService _dialogService;
private readonly INavigationService _navigationService;
private readonly ILogger<MainViewModel> _logger; private readonly ILogger<MainViewModel> _logger;
/// <summary> /// <summary>
/// 当前显示的视图模型。 /// 当前显示的视图模型。
/// </summary> /// </summary>
[ObservableProperty] [ObservableProperty]
private ViewModelBase currentViewModel; private ViewModelBase _currentViewModel;
/// <summary> /// <summary>
/// 应用程序的菜单列表。 /// 应用程序的菜单列表。
@@ -48,10 +50,11 @@ public partial class MainViewModel : ViewModelBase
/// <param name="dataServices">数据服务。</param> /// <param name="dataServices">数据服务。</param>
/// <param name="dialogService">对话框服务。</param> /// <param name="dialogService">对话框服务。</param>
/// <param name="logger">日志记录器。</param> /// <param name="logger">日志记录器。</param>
public MainViewModel(DataServices dataServices, public MainViewModel(DataServices dataServices,INavigationService navigationService,
ILogger<MainViewModel> logger) ILogger<MainViewModel> logger)
{ {
DataServices = dataServices; DataServices = dataServices;
_navigationService = navigationService;
_logger = logger; _logger = logger;
CurrentViewModel = new HomeViewModel(); CurrentViewModel = new HomeViewModel();
@@ -161,8 +164,9 @@ public partial class MainViewModel : ViewModelBase
/// 处理菜单选择变化的逻辑。 /// 处理菜单选择变化的逻辑。
/// </summary> /// </summary>
/// <param name="menu">被选中的菜单项。</param> /// <param name="menu">被选中的菜单项。</param>
public async Task MenuSelectionChanged(MenuBean menu) public async Task MenuSelectionChanged(MenuBeanItemViewModel menu)
{ {
_navigationService.NavigateToAsync(menu.TargetViewKey);
// try // try
// { // {
// switch (menu.Type) // switch (menu.Type)

View File

@@ -50,13 +50,12 @@ public partial class MqttsViewModel : ViewModelBase
private MqttServerItemViewModel _selectedMqtt; private MqttServerItemViewModel _selectedMqtt;
public MqttsViewModel( public MqttsViewModel(
ILogger<MqttsViewModel> logger, IDialogService dialogService, DataServices dataServices, NavgatorServices navgatorServices ILogger<MqttsViewModel> logger, IDialogService dialogService, DataServices dataServices
) )
{ {
_logger = logger; _logger = logger;
_dialogService = dialogService; _dialogService = dialogService;
_dataServices = dataServices; _dataServices = dataServices;
_navgatorServices = navgatorServices;
// if (dataServices.Mqtts == null || dataServices.Mqtts.Count == 0) // if (dataServices.Mqtts == null || dataServices.Mqtts.Count == 0)
// { // {

View File

@@ -8,6 +8,7 @@
xmlns:vm="clr-namespace:DMS.WPF.ViewModels" xmlns:vm="clr-namespace:DMS.WPF.ViewModels"
xmlns:mo="clr-namespace:DMS.Core.Models;assembly=DMS.Core" xmlns:mo="clr-namespace:DMS.Core.Models;assembly=DMS.Core"
xmlns:hc="https://handyorg.github.io/handycontrol" xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:tb="http://hardcodet.net/taskbar" xmlns:tb="http://hardcodet.net/taskbar"
xmlns:taskbarNotification="http://www.hardcodet.net/taskbar" xmlns:taskbarNotification="http://www.hardcodet.net/taskbar"
Title="设备管理系统" Title="设备管理系统"
@@ -32,7 +33,7 @@
<DataTemplate x:Key="NavigationViewMenuItem" <DataTemplate x:Key="NavigationViewMenuItem"
DataType="{x:Type mo:MenuBean}"> DataType="{x:Type mo:MenuBean}">
<ui:NavigationViewItem Content="{Binding Header}" <ui:NavigationViewItem Content="{Binding Header}"
Tag="{Binding }"
MenuItemsSource="{Binding Children }"> MenuItemsSource="{Binding Children }">
<ui:NavigationViewItem.Icon> <ui:NavigationViewItem.Icon>
<ui:FontIcon Glyph="{Binding Icon}" /> <ui:FontIcon Glyph="{Binding Icon}" />
@@ -61,9 +62,10 @@
IsBackButtonVisible="Collapsed" IsBackButtonVisible="Collapsed"
IsBackEnabled="False" IsBackEnabled="False"
SelectionFollowsFocus="Disabled" SelectionFollowsFocus="Disabled"
SelectionChanged="NavigationView_SelectionChanged"
MenuItemsSource="{Binding DataServices.MenuTrees}" MenuItemsSource="{Binding DataServices.MenuTrees}"
MenuItemTemplate="{StaticResource NavigationViewMenuItem}" MenuItemTemplate="{StaticResource NavigationViewMenuItem}">
SelectionChanged="NavigationView_SelectionChanged">
<ui:NavigationView.AutoSuggestBox> <ui:NavigationView.AutoSuggestBox>

View File

@@ -5,6 +5,7 @@ using DMS.WPF.ViewModels;
using iNKORE.UI.WPF.Modern.Controls; using iNKORE.UI.WPF.Modern.Controls;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using DMS.Core.Enums; using DMS.Core.Enums;
using DMS.WPF.ViewModels.Items;
namespace DMS.WPF.Views; namespace DMS.WPF.Views;
@@ -58,15 +59,12 @@ public partial class MainView : Window
/// <param name="args"></param> /// <param name="args"></param>
private async void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args) private async void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{ {
// var menu = args.SelectedItem as MenuBean; var menu = args.SelectedItem as MenuBeanItemViewModel;
// if (menu != null) if (menu != null)
// { {
// await _viewModel.MenuSelectionChanged(menu); await _viewModel.MenuSelectionChanged(menu);
// } }
// else
// {
// NotificationHelper.ShowError("选择的菜单项为空!");
// }
} }
private async void MainView_OnLoaded(object sender, RoutedEventArgs e) private async void MainView_OnLoaded(object sender, RoutedEventArgs e)