From dbf7b81d4b827c770826e10a5d941c8ce9b7eb0f Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Wed, 2 Jul 2025 12:01:20 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=99=E5=BA=94=E7=94=A8=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=9B=BE=E6=A0=87=EF=BC=8C=E5=AE=8C=E6=88=90=E7=BB=99=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E6=B7=BB=E5=8A=A0=E5=8F=98=E9=87=8F=E8=A1=A8=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.xaml | 1 + Assets/AppIcon.png | Bin 0 -> 2650 bytes Data/Repositories/MenuRepository.cs | 2 +- Data/Repositories/VarTableRepository.cs | 21 +++ Helper/DataServicesHelper.cs | 35 +++++ Message/LoadMessage.cs | 2 + Models/VariableTable.cs | 12 +- PMSWPF.csproj | 5 + Services/DataServices.cs | 106 ++------------ Services/DialogService.cs | 15 ++ Services/IDialogService.cs | 2 + ViewModels/DevicesViewModel.cs | 20 ++- ViewModels/Dialogs/VarTableDialogViewModel.cs | 14 ++ ViewModels/MainViewModel.cs | 130 ++++++++++++++++-- ViewModels/VariableTableViewModel.cs | 4 +- Views/Dialogs/ConfirmDialog.xaml | 6 - Views/Dialogs/VarTableDialog.xaml | 40 ++++++ Views/Dialogs/VarTableDialog.xaml.cs | 13 ++ Views/MainView.xaml | 10 +- Views/MainView.xaml.cs | 39 ++---- 20 files changed, 326 insertions(+), 151 deletions(-) create mode 100644 Assets/AppIcon.png create mode 100644 Data/Repositories/VarTableRepository.cs create mode 100644 ViewModels/Dialogs/VarTableDialogViewModel.cs create mode 100644 Views/Dialogs/VarTableDialog.xaml create mode 100644 Views/Dialogs/VarTableDialog.xaml.cs diff --git a/App.xaml b/App.xaml index aaa37d3..aa6b598 100644 --- a/App.xaml +++ b/App.xaml @@ -2,6 +2,7 @@ x:Class="PMSWPF.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"> diff --git a/Assets/AppIcon.png b/Assets/AppIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..303c6226ee2b3bb4b7de16531939131e04ae9848 GIT binary patch literal 2650 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 IM6N<$g3A>7D*ylh literal 0 HcmV?d00001 diff --git a/Data/Repositories/MenuRepository.cs b/Data/Repositories/MenuRepository.cs index 5e28994..a804b79 100644 --- a/Data/Repositories/MenuRepository.cs +++ b/Data/Repositories/MenuRepository.cs @@ -40,7 +40,7 @@ public class MenuRepository } - public async Task AddMenu(MenuBean menu) + public async Task Add(MenuBean menu) { using (var db = DbContext.GetInstance()) { diff --git a/Data/Repositories/VarTableRepository.cs b/Data/Repositories/VarTableRepository.cs new file mode 100644 index 0000000..3a2eb46 --- /dev/null +++ b/Data/Repositories/VarTableRepository.cs @@ -0,0 +1,21 @@ +using PMSWPF.Data.Entities; +using PMSWPF.Extensions; +using PMSWPF.Models; + +namespace PMSWPF.Data.Repositories; + +public class VarTableRepository +{ + /// + /// 添加变量表 + /// + /// + /// 变量表的ID + public async Task Add(VariableTable varTable) + { + using (var db = DbContext.GetInstance()) + { + return await db.Insertable(varTable.CopyTo()).ExecuteReturnIdentityAsync(); + } + } +} \ No newline at end of file diff --git a/Helper/DataServicesHelper.cs b/Helper/DataServicesHelper.cs index 6581b42..7e2e7b9 100644 --- a/Helper/DataServicesHelper.cs +++ b/Helper/DataServicesHelper.cs @@ -7,6 +7,24 @@ namespace PMSWPF.Helper; public class DataServicesHelper { + + /// + /// 从设备列表中找到变量表VarTable对象 + /// + /// VarTable的ID + /// 如果找到择返回对象,否则返回null + public static VariableTable FindVarTableForDevice(List devices, int vtableId) + { + VariableTable varTable = null; + foreach (var device in devices) + { + varTable = device.VariableTables.FirstOrDefault(v => v.Id == vtableId); + if (varTable != null) + return varTable; + } + + return varTable; + } public static MenuBean FindMenusForDevice(Device device,List menus) { foreach (var mainMenu in menus) @@ -24,6 +42,23 @@ public class DataServicesHelper return null; } + /// + /// 给菜单排序 + /// + /// + public static void SortMenus(MenuBean menu) + { + if (menu.Items == null || menu.Items.Count() == 0) + return; + menu.Items.Sort((a, b) => + a.Type.ToString().Length.CompareTo(b.Type.ToString().Length) + ); + foreach (var menuItem in menu.Items) + { + SortMenus(menuItem); + } + } + public static ViewModelBase GetMainViewModel(string name) { ViewModelBase navgateVM = App.Current.Services.GetRequiredService(); diff --git a/Message/LoadMessage.cs b/Message/LoadMessage.cs index c743844..72ac1d8 100644 --- a/Message/LoadMessage.cs +++ b/Message/LoadMessage.cs @@ -5,7 +5,9 @@ namespace PMSWPF.Message; public class LoadMessage:ValueChangedMessage { + public LoadTypes LoadType { get; set; } public LoadMessage(LoadTypes types) : base(types) { + LoadType=types; } } \ No newline at end of file diff --git a/Models/VariableTable.cs b/Models/VariableTable.cs index ce5ed1a..2e96e0e 100644 --- a/Models/VariableTable.cs +++ b/Models/VariableTable.cs @@ -1,13 +1,17 @@ -using PMSWPF.Enums; +using CommunityToolkit.Mvvm.ComponentModel; +using PMSWPF.Enums; namespace PMSWPF.Models; -public class VariableTable +public partial class VariableTable:ObservableObject { public int Id { get; set; } - public string Name { get; set; } - public string Description { get; set; } + [ObservableProperty] private string name; + [ObservableProperty] private string description; + public ProtocolType ProtocolType { get; set; } public List DataVariables { get; set; } + + public int? DeviceId { get; set; } public Device? Device { get; set; } } \ No newline at end of file diff --git a/PMSWPF.csproj b/PMSWPF.csproj index 84c5d6c..539810a 100644 --- a/PMSWPF.csproj +++ b/PMSWPF.csproj @@ -6,6 +6,7 @@ enable enable true + AppIcon2.ico @@ -28,5 +29,9 @@ + + + + diff --git a/Services/DataServices.cs b/Services/DataServices.cs index bf9f4cd..8f59e7b 100644 --- a/Services/DataServices.cs +++ b/Services/DataServices.cs @@ -21,6 +21,7 @@ public partial class DataServices : ObservableRecipient, IRecipient private readonly DeviceRepository _deviceRepository; private readonly MenuRepository _menuRepository; + public event Action> OnDeviceListChanged; public event Action> OnMenuTreeListChanged; @@ -28,19 +29,11 @@ public partial class DataServices : ObservableRecipient, IRecipient partial void OnDevicesChanged(List devices) { OnDeviceListChanged?.Invoke(devices); - if (menuTrees != null && Devices != null) - { - FillMenuData(MenuTrees, Devices); - } } partial void OnMenuTreesChanged(List MenuTrees) { OnMenuTreeListChanged?.Invoke(MenuTrees); - if (MenuTrees != null && Devices != null) - { - FillMenuData(MenuTrees, Devices); - } } @@ -53,77 +46,6 @@ public partial class DataServices : ObservableRecipient, IRecipient } - /// - /// 给Menu菜单的Data填充数据 - /// - /// - private void FillMenuData(List MenuTrees, List devices) - { - if (MenuTrees == null || MenuTrees.Count == 0) - return; - - foreach (MenuBean menuBean in MenuTrees) - { - switch (menuBean.Type) - { - case MenuType.MainMenu: - menuBean.ViewModel =DataServicesHelper.GetMainViewModel(menuBean.Name); - break; - case MenuType.DeviceMenu: - menuBean.ViewModel = App.Current.Services.GetRequiredService(); - menuBean.Data = devices.FirstOrDefault(d => d.Id == menuBean.DataId); - break; - case MenuType.VariableTableMenu: - var varTableVM = App.Current.Services.GetRequiredService(); - varTableVM.VariableTable = FindVarTableForDevice(menuBean.DataId); - menuBean.ViewModel = varTableVM; - menuBean.Data = varTableVM.VariableTable; - break; - case MenuType.AddVariableTableMenu: - break; - } - - if (menuBean.Items != null && menuBean.Items.Count > 0) - { - FillMenuData(menuBean.Items, devices); - } - } - } - - - - /// - /// 查找设备所对应的菜单对象 - /// - /// - /// - public async Task UpdateMenuForDevice(Device device) - { - var menu= DataServicesHelper.FindMenusForDevice(device, MenuTrees); - if (menu != null) - return await _menuRepository.Edit(menu); - - return 0; - } - - /// - /// 从设备列表中找到变量表VarTable对象 - /// - /// VarTable的ID - /// 如果找到择返回对象,否则返回null - private VariableTable FindVarTableForDevice(int vtableId) - { - VariableTable varTable = null; - foreach (var device in _devices) - { - varTable = device.VariableTables.FirstOrDefault(v => v.Id == vtableId); - if (varTable != null) - return varTable; - } - - return varTable; - } - /// /// 接受加载消息,收到消息后从数据库加载对应的数据 /// @@ -131,18 +53,16 @@ public partial class DataServices : ObservableRecipient, IRecipient /// public async void Receive(LoadMessage message) { - if (!(message.Value is LoadTypes)) - throw new ArgumentException($"接受到的加载类型错误:{message.Value}"); try { - switch ((LoadTypes)message.Value) + switch (message.LoadType) { case LoadTypes.All: - Devices = await _deviceRepository.GetAll(); + await LoadDevices(); await LoadMenus(); break; case LoadTypes.Devices: - Devices = await _deviceRepository.GetAll(); + await LoadDevices(); break; case LoadTypes.Menu: await LoadMenus(); @@ -156,24 +76,18 @@ public partial class DataServices : ObservableRecipient, IRecipient } } + private async Task LoadDevices() + { + Devices = await _deviceRepository.GetAll(); + } + private async Task LoadMenus() { MenuTrees = await _menuRepository.GetMenuTrees(); foreach (MenuBean menu in MenuTrees) { MenuHelper.MenuAddParent(menu); + DataServicesHelper.SortMenus(menu); } } - - public async Task DeleteMenuForDevice(Device device) - { - var menu= DataServicesHelper.FindMenusForDevice(device, MenuTrees); - if (menu != null) - { - return await _menuRepository.DeleteMenu(menu); - } - - return 0; - - } } \ No newline at end of file diff --git a/Services/DialogService.cs b/Services/DialogService.cs index 5333139..2640327 100644 --- a/Services/DialogService.cs +++ b/Services/DialogService.cs @@ -56,6 +56,21 @@ public class DialogService :IDialogService return false; } + public async Task ShowAddVarTableDialog(Device device) + { + VarTableDialogViewModel vm = new(); + vm.Title = "添加变量表"; + vm.PrimaryButtonText = "添加变量表"; + vm.VariableTable = new VariableTable(); + var dialog = new VarTableDialog(vm); + var res = await dialog.ShowAsync(); + if (res == ContentDialogResult.Primary) + { + return vm.VariableTable; + } + return null; + } + public void ShowMessageDialog(string title, string message) { diff --git a/Services/IDialogService.cs b/Services/IDialogService.cs index 46e9cb0..cfaea5c 100644 --- a/Services/IDialogService.cs +++ b/Services/IDialogService.cs @@ -8,6 +8,8 @@ public interface IDialogService Task ShowEditDeviceDialog(Device device); Task ShowConfrimeDialog(string title, string message,string buttonText="确认"); + + Task ShowAddVarTableDialog(Device device); void ShowMessageDialog(string title, string message); } \ No newline at end of file diff --git a/ViewModels/DevicesViewModel.cs b/ViewModels/DevicesViewModel.cs index 84c759c..97756da 100644 --- a/ViewModels/DevicesViewModel.cs +++ b/ViewModels/DevicesViewModel.cs @@ -102,9 +102,12 @@ public partial class DevicesViewModel : ViewModelBase var editDievce = await _dialogService.ShowEditDeviceDialog(SelectedDevice); if (editDievce != null) - { + { + // 更新菜单 var res = await _deviceRepository.Edit(editDievce); - await _dataServices.UpdateMenuForDevice(editDievce); + var menu = DataServicesHelper.FindMenusForDevice(editDievce, _dataServices.MenuTrees); + if (menu != null) + await _menuRepository.Edit(menu); MessageHelper.SendLoadMessage(LoadTypes.Menu); MessageHelper.SendLoadMessage(LoadTypes.Devices); @@ -132,8 +135,13 @@ public partial class DevicesViewModel : ViewModelBase var isDel = await _dialogService.ShowConfrimeDialog("删除设备", msg, "删除设备"); if (isDel) { - var defDeviceRes = await _deviceRepository.DeleteById(SelectedDevice.Id); - var defMenuRes = await _dataServices.DeleteMenuForDevice(SelectedDevice); + // 删除设备 + await _deviceRepository.DeleteById(SelectedDevice.Id); + // 删除菜单 + var menu = DataServicesHelper.FindMenusForDevice(SelectedDevice, _dataServices.MenuTrees); + if (menu != null) + await _menuRepository.DeleteMenu(menu); + MessageHelper.SendLoadMessage(LoadTypes.Menu); MessageHelper.SendLoadMessage(LoadTypes.Devices); NotificationHelper.ShowMessage($"删除设备成功,设备名:{SelectedDevice.Name}", NotificationType.Success); @@ -141,8 +149,8 @@ public partial class DevicesViewModel : ViewModelBase } catch (Exception e) { - NotificationHelper.ShowMessage($"编辑设备的过程中发生错误:{e.Message}", NotificationType.Error); - _logger.LogError($"编辑设备的过程中发生错误:{e}"); + NotificationHelper.ShowMessage($"删除设备的过程中发生错误:{e.Message}", NotificationType.Error); + _logger.LogError($"删除设备的过程中发生错误:{e}"); } } diff --git a/ViewModels/Dialogs/VarTableDialogViewModel.cs b/ViewModels/Dialogs/VarTableDialogViewModel.cs new file mode 100644 index 0000000..e59461f --- /dev/null +++ b/ViewModels/Dialogs/VarTableDialogViewModel.cs @@ -0,0 +1,14 @@ +using CommunityToolkit.Mvvm.ComponentModel; +using PMSWPF.Models; + +namespace PMSWPF.ViewModels.Dialogs; + +public partial class VarTableDialogViewModel:ObservableObject +{ + [ObservableProperty] + private VariableTable variableTable; + [ObservableProperty] + private string title; + [ObservableProperty] + private string primaryButtonText; +} \ No newline at end of file diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs index ddcf046..c377293 100644 --- a/ViewModels/MainViewModel.cs +++ b/ViewModels/MainViewModel.cs @@ -1,5 +1,9 @@ using System.Collections.ObjectModel; using CommunityToolkit.Mvvm.ComponentModel; +using iNKORE.UI.WPF.Modern.Common.IconKeys; +using iNKORE.UI.WPF.Modern.Controls; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using PMSWPF.Data.Repositories; using PMSWPF.Enums; using PMSWPF.Helper; @@ -12,35 +16,143 @@ public partial class MainViewModel : ViewModelBase { private readonly NavgatorServices _navgatorServices; private readonly DataServices _dataServices; + private readonly IDialogService _dialogService; + private readonly ILogger _logger; + [ObservableProperty] private ViewModelBase currentViewModel; - [ObservableProperty] - private ObservableCollection _menus; + [ObservableProperty] private ObservableCollection _menus; private readonly MenuRepository _menuRepository; - public MainViewModel(NavgatorServices navgatorServices,DataServices dataServices) + + public MainViewModel(NavgatorServices navgatorServices, DataServices dataServices, IDialogService dialogService, + ILogger logger) { _navgatorServices = navgatorServices; _dataServices = dataServices; + _dialogService = dialogService; + _logger = logger; + _navgatorServices.OnViewModelChanged += () => { CurrentViewModel = _navgatorServices.CurrentViewModel; }; - + CurrentViewModel = new HomeViewModel(); CurrentViewModel.OnLoaded(); // 发送消息加载数据 MessageHelper.SendLoadMessage(LoadTypes.All); // 当菜单加载成功后,在前台显示菜单 - dataServices.OnMenuTreeListChanged += (menus) => - { - Menus = new ObservableCollection(menus); - }; + dataServices.OnMenuTreeListChanged += (menus) => { Menus = new ObservableCollection(menus); }; + } + public async Task MenuSelectionChanged(MenuBean menu) + { + try + { + switch (menu.Type) + { + case MenuType.MainMenu: + menu.ViewModel = DataServicesHelper.GetMainViewModel(menu.Name); + break; + case MenuType.DeviceMenu: + menu.ViewModel = App.Current.Services.GetRequiredService(); + menu.Data = _dataServices.Devices.FirstOrDefault(d => d.Id == menu.DataId); + break; + case MenuType.VariableTableMenu: + + VariableTableViewModel varTableVM = App.Current.Services.GetRequiredService(); + varTableVM.VariableTable = + DataServicesHelper.FindVarTableForDevice(_dataServices.Devices, menu.DataId); + menu.ViewModel = varTableVM; + menu.Data = varTableVM.VariableTable; + + break; + case MenuType.AddVariableTableMenu: + await AddVariableTable(menu); + break; + } + + if (menu.Type==MenuType.AddVariableTableMenu) + return; + + if (menu.ViewModel != null) + { + MessageHelper.SendNavgatorMessage(menu.ViewModel); + _logger.LogInformation($"导航到:{menu.Name}"); + } + else + { + NotificationHelper.ShowMessage($"菜单:{menu.Name},没有对应的ViewModel."); + _logger.LogInformation($"菜单:{menu.Name},没有对应的ViewModel."); + } + } + catch (Exception e) + { + _logger.LogError($"菜单切换是出现了错误:{e}"); + NotificationHelper.ShowMessage($"菜单切换是出现了错误:{e.Message}", NotificationType.Error); + } + } + + + private async Task AddVariableTable(MenuBean menu) + { + try + { + if (menu.Parent != null && menu.Parent.Data != null) + { + Device device = (Device)menu.Parent.Data; + var varTable = await _dialogService.ShowAddVarTableDialog(device); + if (varTable != null) + { + // 添加变量表 + varTable.DeviceId = device.Id; + varTable.ProtocolType = device.ProtocolType; + var addVarTableId = await new VarTableRepository().Add(varTable); + if (addVarTableId > 0) + { + // 添加变量表菜单 + MenuBean newMenu = new MenuBean(); + newMenu.Icon = SegoeFluentIcons.Tablet.Glyph; + newMenu.Name = varTable.Name; + newMenu.DataId = addVarTableId; + newMenu.Type = MenuType.VariableTableMenu; + newMenu.ParentId = menu.Parent.Id; + var addMenuRes = await new MenuRepository().Add(newMenu); + if (addMenuRes > 0) + { + // 变量表菜单添加成功 + MessageHelper.SendLoadMessage(LoadTypes.Menu); + MessageHelper.SendLoadMessage(LoadTypes.Devices); + NotificationHelper.ShowMessage($"变量表:{varTable.Name},添加成功", + NotificationType.Success); + _logger.LogInformation($"变量表:{varTable.Name},添加成功"); + } + else + { + // 变量表菜单添加失败 + NotificationHelper.ShowMessage($"变量表:{varTable.Name},添加菜单失败", + NotificationType.Error); + _logger.LogError($"变量表:{varTable.Name},添加菜单失败"); + } + } + else + { + // 变量表添加失败 + NotificationHelper.ShowMessage($"变量表:{varTable.Name},添加失败", NotificationType.Error); + _logger.LogError($"变量表:{varTable.Name},添加失败"); + } + } + } + } + catch (Exception e) + { + _logger.LogError($"添加变量表时出现了错误:{e}"); + NotificationHelper.ShowMessage($"添加变量表时出现了错误:{e.Message}", NotificationType.Error); + } } public override void OnLoaded() { - } } \ No newline at end of file diff --git a/ViewModels/VariableTableViewModel.cs b/ViewModels/VariableTableViewModel.cs index 6a7b02d..655dcb9 100644 --- a/ViewModels/VariableTableViewModel.cs +++ b/ViewModels/VariableTableViewModel.cs @@ -10,12 +10,12 @@ partial class VariableTableViewModel : ViewModelBase private VariableTable variableTable; [ObservableProperty] private ObservableCollection _dataVariables; - + public override void OnLoaded() { if (VariableTable.DataVariables!=null ) { - _dataVariables = new ObservableCollection(VariableTable.DataVariables); + DataVariables = new ObservableCollection(VariableTable.DataVariables); } } } \ No newline at end of file diff --git a/Views/Dialogs/ConfirmDialog.xaml b/Views/Dialogs/ConfirmDialog.xaml index 47bb270..ec6a0ea 100644 --- a/Views/Dialogs/ConfirmDialog.xaml +++ b/Views/Dialogs/ConfirmDialog.xaml @@ -17,12 +17,6 @@ d:DataContext="{d:DesignInstance vmd:ConfrimDialogViewModel}" mc:Ignorable="d"> - - - - diff --git a/Views/Dialogs/VarTableDialog.xaml b/Views/Dialogs/VarTableDialog.xaml new file mode 100644 index 0000000..d5f5e50 --- /dev/null +++ b/Views/Dialogs/VarTableDialog.xaml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + diff --git a/Views/Dialogs/VarTableDialog.xaml.cs b/Views/Dialogs/VarTableDialog.xaml.cs new file mode 100644 index 0000000..8b502f3 --- /dev/null +++ b/Views/Dialogs/VarTableDialog.xaml.cs @@ -0,0 +1,13 @@ +using iNKORE.UI.WPF.Modern.Controls; +using PMSWPF.ViewModels.Dialogs; + +namespace PMSWPF.Views.Dialogs; + +public partial class VarTableDialog : ContentDialog +{ + public VarTableDialog(VarTableDialogViewModel viewModel) + { + InitializeComponent(); + this.DataContext = viewModel; + } +} \ No newline at end of file diff --git a/Views/MainView.xaml b/Views/MainView.xaml index c7202b7..0dd6f67 100644 --- a/Views/MainView.xaml +++ b/Views/MainView.xaml @@ -19,9 +19,17 @@ d:DataContext="{d:DesignInstance vm:MainViewModel}" mc:Ignorable="d"> + + + + + + + - diff --git a/Views/MainView.xaml.cs b/Views/MainView.xaml.cs index e878fcd..12cb8c6 100644 --- a/Views/MainView.xaml.cs +++ b/Views/MainView.xaml.cs @@ -1,9 +1,11 @@ using System.Windows; using iNKORE.UI.WPF.Modern.Controls; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using PMSWPF.Enums; using PMSWPF.Helper; using PMSWPF.Models; +using PMSWPF.Services; using PMSWPF.ViewModels; namespace PMSWPF.Views; @@ -13,15 +15,17 @@ namespace PMSWPF.Views; /// public partial class MainView : Window { + private readonly DataServices _dataServices; private readonly ILogger _logger; private MainViewModel _viewModel; - public MainView(MainViewModel viewModel, ILogger logger) + public MainView(DataServices dataServices, ILogger logger) { InitializeComponent(); - _viewModel = viewModel; + _viewModel = App.Current.Services.GetRequiredService(); + _dataServices = dataServices; _logger = logger; - DataContext = viewModel; + DataContext = _viewModel; _logger.LogInformation("主界面加载成功"); } @@ -30,32 +34,16 @@ public partial class MainView : Window /// /// /// - private void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args) + private async void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args) { - try + var menu = args.SelectedItem as MenuBean; + if (menu != null) { - var menu = args.SelectedItem as MenuBean; - if (menu == null) - throw new ArgumentException("选择的菜单项为空!"); - - switch (menu.Type) - { - case MenuType.MainMenu: - case MenuType.DeviceMenu: - case MenuType.VariableTableMenu: - if (menu.ViewModel == null) - throw new ArgumentException($"菜单项:{menu.Name},没有绑定对象的ViewModel"); - MessageHelper.SendNavgatorMessage(menu.ViewModel); - _logger.LogInformation($"导航到:{menu.Name}"); - break; - case MenuType.AddVariableTableMenu: - break; - } + await _viewModel.MenuSelectionChanged(menu); } - catch (Exception e) + else { - NotificationHelper.ShowMessage(e.Message, NotificationType.Error); - _logger.LogError(e.Message); + NotificationHelper.ShowMessage("选择的菜单项为空!", NotificationType.Error); } } @@ -63,5 +51,4 @@ public partial class MainView : Window { _viewModel.OnLoaded(); } - } \ No newline at end of file