diff --git a/DMS.Application/DTOs/MenuBeanDto.cs b/DMS.Application/DTOs/MenuBeanDto.cs
index 6198319..9e08565 100644
--- a/DMS.Application/DTOs/MenuBeanDto.cs
+++ b/DMS.Application/DTOs/MenuBeanDto.cs
@@ -19,7 +19,11 @@ public class MenuBeanDto
///
/// 菜单关联的数据ID,例如设备Id,变量表Id
///
- public int TargetId { get; set; }
+ public int TargetId { get; set; }
+ ///
+ /// 菜单关联的数据ID,例如设备Id,变量表Id
+ ///
+ public string TargetViewKey { get; set; }
public string NavigationParameter { get; set; }
public int DisplayOrder { get; set; }
}
\ No newline at end of file
diff --git a/DMS.Core/Models/MenuBean.cs b/DMS.Core/Models/MenuBean.cs
index 35ded1b..f61b85c 100644
--- a/DMS.Core/Models/MenuBean.cs
+++ b/DMS.Core/Models/MenuBean.cs
@@ -19,7 +19,11 @@ public class MenuBean
///
/// 菜单关联的数据ID,例如设备Id,变量表Id
///
- public int TargetId { get; set; }
+ public int TargetId { get; set; }
+ ///
+ /// 菜单关联的数据ID,例如设备Id,变量表Id
+ ///
+ public string TargetViewKey { get; set; }
public string NavigationParameter { get; set; }
public int DisplayOrder { get; set; }
}
\ No newline at end of file
diff --git a/DMS.Infrastructure.UnitTests/FakerHelper.cs b/DMS.Infrastructure.UnitTests/FakerHelper.cs
index 7da45c0..d2e2627 100644
--- a/DMS.Infrastructure.UnitTests/FakerHelper.cs
+++ b/DMS.Infrastructure.UnitTests/FakerHelper.cs
@@ -137,7 +137,6 @@ namespace DMS.Infrastructure.UnitTests
.RuleFor(m => m.Icon, f => f.Random.Word())
.RuleFor(m => m.DisplayOrder, f => f.Random.Number(1, 100))
.RuleFor(m => m.ParentId, f => f.Random.Number(0, 10)) // 假设可以有父菜单
- .RuleFor(m => m.MenuType, f => f.PickRandom()) // 假设 MenuType 是一个枚举
.Generate();
return menuDto;
}
diff --git a/DMS.Infrastructure/Entities/DbMenu.cs b/DMS.Infrastructure/Entities/DbMenu.cs
index e8f2abb..7ca5351 100644
--- a/DMS.Infrastructure/Entities/DbMenu.cs
+++ b/DMS.Infrastructure/Entities/DbMenu.cs
@@ -41,6 +41,10 @@ public class DbMenu
/// 菜单关联的数据ID,例如设备Id,变量表Id
///
public int TargetId { get; set; }
+ ///
+ /// 菜单关联的数据ID,例如设备Id,变量表Id
+ ///
+ public string TargetViewKey { get; set; }
///
/// 导航的可选参数
diff --git a/DMS.Infrastructure/Repositories/InitializeRepository.cs b/DMS.Infrastructure/Repositories/InitializeRepository.cs
index 65422c7..0a2aad4 100644
--- a/DMS.Infrastructure/Repositories/InitializeRepository.cs
+++ b/DMS.Infrastructure/Repositories/InitializeRepository.cs
@@ -24,7 +24,7 @@ public class InitializeRepository : IInitializeRepository
public InitializeRepository(SqlSugarDbContext dbContext)
{
_dbContext = dbContext;
- _db = _dbContext.GetInstance();
+ _db = _dbContext.GetInstance();
}
///
@@ -44,7 +44,7 @@ public class InitializeRepository : IInitializeRepository
_db.CodeFirst.InitTables();
_db.CodeFirst.InitTables();
}
-
+
///
/// 初始化数据库表索引。
/// 为特定表的列创建唯一索引,以提高查询效率和数据完整性。
@@ -52,22 +52,22 @@ public class InitializeRepository : IInitializeRepository
public void InitializeTableIndex()
{
// 为 DbDevice 表创建索引
- _db.DbMaintenance.CreateIndex(nameof(DbDevice),new []
- {
- nameof(DbDevice.Name),
- nameof(DbDevice.OpcUaServerUrl),
- },true);
-
+ _db.DbMaintenance.CreateIndex(nameof(DbDevice), new[]
+ {
+ nameof(DbDevice.Name),
+ nameof(DbDevice.OpcUaServerUrl),
+ }, true);
+
// 为 DbVariable 表创建索引
- _db.DbMaintenance.CreateIndex(nameof(DbVariable),new []
- {
- nameof(DbVariable.OpcUaNodeId)
- },true);
+ _db.DbMaintenance.CreateIndex(nameof(DbVariable), new[]
+ {
+ nameof(DbVariable.OpcUaNodeId)
+ }, true);
// 为 DbMqttServer 表创建索引
- _db.DbMaintenance.CreateIndex(nameof(DbMqttServer),new []
- {
- nameof(DbMqttServer.ServerName)
- },true);
+ _db.DbMaintenance.CreateIndex(nameof(DbMqttServer), new[]
+ {
+ nameof(DbMqttServer.ServerName)
+ }, true);
}
///
@@ -77,7 +77,7 @@ public class InitializeRepository : IInitializeRepository
/// 如果表存在则为 true,否则为 false。
public bool IsAnyTable(string tableName)
{
- return _db.DbMaintenance.IsAnyTable(tableName, false);
+ return _db.DbMaintenance.IsAnyTable(tableName, false);
}
///
@@ -87,7 +87,7 @@ public class InitializeRepository : IInitializeRepository
/// 如果索引存在则为 true,否则为 false。
public bool IsAnyIndex(string indexName)
{
- return _db.DbMaintenance.IsAnyIndex(indexName);
+ return _db.DbMaintenance.IsAnyIndex(indexName);
}
///
@@ -97,23 +97,53 @@ public class InitializeRepository : IInitializeRepository
public void InitializeMenus()
{
// 检查数据库中是否已存在菜单数据
- if (_db.Queryable().Any())
+ if (_db.Queryable()
+ .Any())
{
return; // 如果数据库中已经有菜单,则不进行初始化
}
// 创建默认菜单项的 DbMenu 实体列表
var defaultMenus = new List
- {
- new DbMenu { Id = 1, Header = "主页", Icon = "\uE80F", ParentId = 0,MenuType = MenuType.MainMenu, DisplayOrder = 1 },
- new DbMenu { Id = 2, Header = "设备", Icon = "\uE975", ParentId = 0,MenuType = MenuType.MainMenu , DisplayOrder = 2 },
- new DbMenu { Id = 3, Header = "数据转换", Icon = "\uF1CB", ParentId = 0,MenuType = MenuType.MainMenu , DisplayOrder = 3 },
- new DbMenu { Id = 4, Header = "Mqtt服务器", Icon = "\uE753", ParentId = 0,MenuType = MenuType.MainMenu , DisplayOrder = 4 },
- new DbMenu { Id = 5, Header = "设置", Icon = "\uE713", ParentId = 0,MenuType = MenuType.MainMenu , DisplayOrder = 5 },
- new DbMenu { Id = 6, Header = "关于", Icon = "\uE946", ParentId = 0, MenuType= MenuType.MainMenu ,DisplayOrder = 6 } // 假设有一个AboutView
- };
+ {
+ new DbMenu
+ {
+ Id = 1, Header = "主页", Icon = "\uE80F", ParentId = 0,
+ MenuType = MenuType.MainMenu, TargetViewKey = "HomeView", DisplayOrder = 1
+ },
+ new DbMenu
+ {
+ Id = 2, Header = "设备", Icon = "\uE975", ParentId = 0,
+ MenuType = MenuType.MainMenu, TargetViewKey = "DevicesView",
+ DisplayOrder = 2
+ },
+ new DbMenu
+ {
+ Id = 3, Header = "数据转换", Icon = "\uF1CB", ParentId = 0,
+ MenuType = MenuType.MainMenu, TargetViewKey = "DataTransformView",
+ DisplayOrder = 3
+ },
+ new DbMenu
+ {
+ Id = 4, Header = "Mqtt服务器", Icon = "\uE753", ParentId = 0,
+ MenuType = MenuType.MainMenu, TargetViewKey = "MqttsView",
+ DisplayOrder = 4
+ },
+ new DbMenu
+ {
+ Id = 5, Header = "设置", Icon = "\uE713", ParentId = 0,
+ MenuType = MenuType.MainMenu, TargetViewKey = "SettingView",
+ DisplayOrder = 5
+ },
+ new DbMenu
+ {
+ Id = 6, Header = "关于", Icon = "\uE946", ParentId = 0,
+ MenuType = MenuType.MainMenu, TargetViewKey = "", DisplayOrder = 6
+ } // 假设有一个AboutView
+ };
// 批量插入菜单到数据库
- _db.Insertable(defaultMenus).ExecuteCommand();
+ _db.Insertable(defaultMenus)
+ .ExecuteCommand();
}
}
\ No newline at end of file
diff --git a/DMS.WPF/Services/DataServices.cs b/DMS.WPF/Services/DataServices.cs
index 9c43736..504d185 100644
--- a/DMS.WPF/Services/DataServices.cs
+++ b/DMS.WPF/Services/DataServices.cs
@@ -56,6 +56,7 @@ public partial class DataServices : ObservableRecipient, IRecipient
public ConcurrentDictionary AllVariables;
private readonly IMenuService _menuService;
+ private readonly INavigationService _navigationService;
// 设备列表变更事件,当设备列表数据更新时触发。
@@ -79,13 +80,14 @@ public partial class DataServices : ObservableRecipient, IRecipient
/// AutoMapper 实例。
///
public DataServices(IMapper mapper, IDeviceAppService deviceAppService,
- IVariableTableAppService variableTableAppService, IVariableAppService variableAppService,IMenuService menuService)
+ IVariableTableAppService variableTableAppService, IVariableAppService variableAppService,IMenuService menuService,INavigationService navigationService)
{
_mapper = mapper;
_deviceAppService = deviceAppService;
_variableTableAppService = variableTableAppService;
_variableAppService = variableAppService;
_menuService = menuService;
+ _navigationService = navigationService;
IsActive = true; // 激活消息接收器
Devices = new ObservableCollection();
VariableTables = new ObservableCollection();
@@ -246,7 +248,7 @@ public partial class DataServices : ObservableRecipient, IRecipient
{
// 这是一个新菜单项,添加到集合中
// 注意:这里直接添加 IMenuService 返回的 MenuItemViewModel 实例
- Menus.Add(_mapper.Map(newDto));
+ Menus.Add(new MenuBeanItemViewModel(newDto,_navigationService));
}
}
diff --git a/DMS.WPF/ViewModels/Items/MenuBeanItemViewModel.cs b/DMS.WPF/ViewModels/Items/MenuBeanItemViewModel.cs
index e041291..9bf9e10 100644
--- a/DMS.WPF/ViewModels/Items/MenuBeanItemViewModel.cs
+++ b/DMS.WPF/ViewModels/Items/MenuBeanItemViewModel.cs
@@ -1,7 +1,10 @@
using System.Collections.ObjectModel;
+using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
+using CommunityToolkit.Mvvm.Input;
using DMS.Application.DTOs;
using DMS.Core.Enums;
+using DMS.WPF.Services;
namespace DMS.WPF.ViewModels.Items;
@@ -23,6 +26,8 @@ public partial class MenuBeanItemViewModel : ObservableObject
[ObservableProperty]
private int _targetId;
+ [ObservableProperty]
+ private string _targetViewKey;
[ObservableProperty]
private string _navigationParameter;
@@ -31,16 +36,26 @@ public partial class MenuBeanItemViewModel : ObservableObject
private int _displayOrder;
[ObservableProperty]
private ObservableCollection _children=new ();
+
+ ///
+ /// 菜单项点击时执行的导航命令。
+ ///
+ public ICommand NavigateCommand { get; }
- public MenuBeanItemViewModel(MenuBeanDto dto)
+ public MenuBeanItemViewModel(MenuBeanDto dto,INavigationService navigationService)
{
Id = dto.Id;
_parentId = dto.ParentId;
_header = dto.Header;
_icon = dto.Icon;
_menuType = dto.MenuType;
+ _targetViewKey=dto.TargetViewKey;
_targetId = dto.TargetId;
_navigationParameter = dto.NavigationParameter;
_displayOrder = dto.DisplayOrder;
+ NavigateCommand = new AsyncRelayCommand(async () =>
+ {
+ await navigationService.NavigateToAsync(_targetViewKey, _navigationParameter);
+ });
}
}
diff --git a/DMS.WPF/Views/MainView.xaml b/DMS.WPF/Views/MainView.xaml
index 59e8ba2..efccbbd 100644
--- a/DMS.WPF/Views/MainView.xaml
+++ b/DMS.WPF/Views/MainView.xaml
@@ -32,6 +32,7 @@