From e509b7de0b6bc532ed9409728475b17b8eb6bae5 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Sat, 26 Jul 2025 18:58:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=A1=B5=E9=9D=A2=E5=AF=BC?= =?UTF-8?q?=E8=88=AA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DMS.WPF/App.xaml.cs | 3 +- DMS.WPF/Services/DialogService.cs | 2 +- DMS.WPF/Services/NavigationService.cs | 40 +++++++++++++++----------- DMS.WPF/ViewModels/DevicesViewModel.cs | 7 ++++- DMS.WPF/ViewModels/MainViewModel.cs | 10 +++++-- DMS.WPF/ViewModels/MqttsViewModel.cs | 3 +- DMS.WPF/Views/MainView.xaml | 8 ++++-- DMS.WPF/Views/MainView.xaml.cs | 16 +++++------ 8 files changed, 53 insertions(+), 36 deletions(-) diff --git a/DMS.WPF/App.xaml.cs b/DMS.WPF/App.xaml.cs index 347e1ad..888a606 100644 --- a/DMS.WPF/App.xaml.cs +++ b/DMS.WPF/App.xaml.cs @@ -161,6 +161,7 @@ public partial class App : System.Windows.Application services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); //注册WPF中的服务 services.AddSingleton(); // 注册视图模型 @@ -174,7 +175,7 @@ public partial class App : System.Windows.Application services.AddSingleton(); //services.AddScoped(); services.AddSingleton(); - services.AddScoped(); + services.AddSingleton(); //注册View视图 services.AddSingleton(); services.AddSingleton(); diff --git a/DMS.WPF/Services/DialogService.cs b/DMS.WPF/Services/DialogService.cs index 0bccccd..22c6181 100644 --- a/DMS.WPF/Services/DialogService.cs +++ b/DMS.WPF/Services/DialogService.cs @@ -8,7 +8,7 @@ using iNKORE.UI.WPF.Modern.Controls; namespace DMS.WPF.Services; -public class DialogService +public class DialogService :IDialogService { // private readonly DataServices _dataServices; // diff --git a/DMS.WPF/Services/NavigationService.cs b/DMS.WPF/Services/NavigationService.cs index efee813..18c63ea 100644 --- a/DMS.WPF/Services/NavigationService.cs +++ b/DMS.WPF/Services/NavigationService.cs @@ -36,9 +36,8 @@ public class NavigationService : INavigationService return; } - var mainViewModel = _serviceProvider.GetRequiredService(); - var viewModelType = GetViewModelTypeByKey(viewKey); - var viewModel = _serviceProvider.GetRequiredService(viewModelType) as ViewModelBase; + var mainViewModel = App.Current.Services.GetRequiredService(); + var viewModel = GetViewModelByKey(viewKey); if (viewModel is INavigatable navigatableViewModel) { @@ -49,19 +48,28 @@ public class NavigationService : INavigationService } - - private Type GetViewModelTypeByKey(string key) + private ViewModelBase GetViewModelByKey(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 的映射配置。") - }; + switch (key) + { + case "HomeView": + return App.Current.Services.GetRequiredService(); + case "DevicesView": + return App.Current.Services.GetRequiredService(); + case "DeviceDetailView": + return App.Current.Services.GetRequiredService(); + case "DataTransformView": + return App.Current.Services.GetRequiredService(); + case "VariableTableView": + return App.Current.Services.GetRequiredService(); + case "MqttsView": + return App.Current.Services.GetRequiredService(); + case "MqttServerDetailView": + return App.Current.Services.GetRequiredService(); + case "SettingView": + return App.Current.Services.GetRequiredService(); + default: + throw new KeyNotFoundException($"未找到与键 '{key}' 关联的视图模型类型。请检查 NavigationService 的映射配置。"); + } } } \ No newline at end of file diff --git a/DMS.WPF/ViewModels/DevicesViewModel.cs b/DMS.WPF/ViewModels/DevicesViewModel.cs index 3815c6d..f1bd0f9 100644 --- a/DMS.WPF/ViewModels/DevicesViewModel.cs +++ b/DMS.WPF/ViewModels/DevicesViewModel.cs @@ -17,7 +17,7 @@ namespace DMS.WPF.ViewModels; /// /// 设备管理视图模型,负责设备的增删改查操作。 /// -public partial class DevicesViewModel : ViewModelBase +public partial class DevicesViewModel : ViewModelBase,INavigatable { private readonly DataServices _dataServices; private readonly IDialogService _dialogService; @@ -197,4 +197,9 @@ public partial class DevicesViewModel : ViewModelBase deviceDetailVm.CurrentDevice = SelectedDevice; MessageHelper.SendNavgatorMessage(deviceDetailVm); } + + public async Task OnNavigatedToAsync(object parameter) + { + + } } \ No newline at end of file diff --git a/DMS.WPF/ViewModels/MainViewModel.cs b/DMS.WPF/ViewModels/MainViewModel.cs index 9ae714c..6560422 100644 --- a/DMS.WPF/ViewModels/MainViewModel.cs +++ b/DMS.WPF/ViewModels/MainViewModel.cs @@ -11,6 +11,7 @@ using DMS.Helper; using DMS.Services; using DMS.ViewModels; using DMS.WPF.Helper; +using DMS.WPF.ViewModels.Items; using iNKORE.UI.WPF.Modern.Common.IconKeys; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -27,13 +28,14 @@ public partial class MainViewModel : ViewModelBase { public DataServices DataServices { get; } private readonly IDialogService _dialogService; + private readonly INavigationService _navigationService; private readonly ILogger _logger; /// /// 当前显示的视图模型。 /// [ObservableProperty] - private ViewModelBase currentViewModel; + private ViewModelBase _currentViewModel; /// /// 应用程序的菜单列表。 @@ -48,10 +50,11 @@ public partial class MainViewModel : ViewModelBase /// 数据服务。 /// 对话框服务。 /// 日志记录器。 - public MainViewModel(DataServices dataServices, + public MainViewModel(DataServices dataServices,INavigationService navigationService, ILogger logger) { DataServices = dataServices; + _navigationService = navigationService; _logger = logger; CurrentViewModel = new HomeViewModel(); @@ -161,8 +164,9 @@ public partial class MainViewModel : ViewModelBase /// 处理菜单选择变化的逻辑。 /// /// 被选中的菜单项。 - public async Task MenuSelectionChanged(MenuBean menu) + public async Task MenuSelectionChanged(MenuBeanItemViewModel menu) { + _navigationService.NavigateToAsync(menu.TargetViewKey); // try // { // switch (menu.Type) diff --git a/DMS.WPF/ViewModels/MqttsViewModel.cs b/DMS.WPF/ViewModels/MqttsViewModel.cs index 934f941..444ae82 100644 --- a/DMS.WPF/ViewModels/MqttsViewModel.cs +++ b/DMS.WPF/ViewModels/MqttsViewModel.cs @@ -50,13 +50,12 @@ public partial class MqttsViewModel : ViewModelBase private MqttServerItemViewModel _selectedMqtt; public MqttsViewModel( - ILogger logger, IDialogService dialogService, DataServices dataServices, NavgatorServices navgatorServices + ILogger logger, IDialogService dialogService, DataServices dataServices ) { _logger = logger; _dialogService = dialogService; _dataServices = dataServices; - _navgatorServices = navgatorServices; // if (dataServices.Mqtts == null || dataServices.Mqtts.Count == 0) // { diff --git a/DMS.WPF/Views/MainView.xaml b/DMS.WPF/Views/MainView.xaml index efccbbd..ee12356 100644 --- a/DMS.WPF/Views/MainView.xaml +++ b/DMS.WPF/Views/MainView.xaml @@ -8,6 +8,7 @@ xmlns:vm="clr-namespace:DMS.WPF.ViewModels" xmlns:mo="clr-namespace:DMS.Core.Models;assembly=DMS.Core" xmlns:hc="https://handyorg.github.io/handycontrol" + xmlns:i="http://schemas.microsoft.com/xaml/behaviors" xmlns:tb="http://hardcodet.net/taskbar" xmlns:taskbarNotification="http://www.hardcodet.net/taskbar" Title="设备管理系统" @@ -32,7 +33,7 @@ @@ -61,10 +62,11 @@ IsBackButtonVisible="Collapsed" IsBackEnabled="False" SelectionFollowsFocus="Disabled" + SelectionChanged="NavigationView_SelectionChanged" MenuItemsSource="{Binding DataServices.MenuTrees}" - MenuItemTemplate="{StaticResource NavigationViewMenuItem}" - SelectionChanged="NavigationView_SelectionChanged"> + MenuItemTemplate="{StaticResource NavigationViewMenuItem}"> + diff --git a/DMS.WPF/Views/MainView.xaml.cs b/DMS.WPF/Views/MainView.xaml.cs index 7160b12..428d7dc 100644 --- a/DMS.WPF/Views/MainView.xaml.cs +++ b/DMS.WPF/Views/MainView.xaml.cs @@ -5,6 +5,7 @@ using DMS.WPF.ViewModels; using iNKORE.UI.WPF.Modern.Controls; using Microsoft.Extensions.DependencyInjection; using DMS.Core.Enums; +using DMS.WPF.ViewModels.Items; namespace DMS.WPF.Views; @@ -58,15 +59,12 @@ public partial class MainView : Window /// private async void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args) { - // var menu = args.SelectedItem as MenuBean; - // if (menu != null) - // { - // await _viewModel.MenuSelectionChanged(menu); - // } - // else - // { - // NotificationHelper.ShowError("选择的菜单项为空!"); - // } + var menu = args.SelectedItem as MenuBeanItemViewModel; + if (menu != null) + { + await _viewModel.MenuSelectionChanged(menu); + } + } private async void MainView_OnLoaded(object sender, RoutedEventArgs e)