给菜单添加TargetViewKey
This commit is contained in:
@@ -19,7 +19,11 @@ public class MenuBeanDto
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 菜单关联的数据ID,例如设备Id,变量表Id
|
/// 菜单关联的数据ID,例如设备Id,变量表Id
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int TargetId { get; set; }
|
public int TargetId { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 菜单关联的数据ID,例如设备Id,变量表Id
|
||||||
|
/// </summary>
|
||||||
|
public string TargetViewKey { get; set; }
|
||||||
public string NavigationParameter { get; set; }
|
public string NavigationParameter { get; set; }
|
||||||
public int DisplayOrder { get; set; }
|
public int DisplayOrder { get; set; }
|
||||||
}
|
}
|
||||||
@@ -19,7 +19,11 @@ public class MenuBean
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 菜单关联的数据ID,例如设备Id,变量表Id
|
/// 菜单关联的数据ID,例如设备Id,变量表Id
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int TargetId { get; set; }
|
public int TargetId { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 菜单关联的数据ID,例如设备Id,变量表Id
|
||||||
|
/// </summary>
|
||||||
|
public string TargetViewKey { get; set; }
|
||||||
public string NavigationParameter { get; set; }
|
public string NavigationParameter { get; set; }
|
||||||
public int DisplayOrder { get; set; }
|
public int DisplayOrder { get; set; }
|
||||||
}
|
}
|
||||||
@@ -137,7 +137,6 @@ namespace DMS.Infrastructure.UnitTests
|
|||||||
.RuleFor(m => m.Icon, f => f.Random.Word())
|
.RuleFor(m => m.Icon, f => f.Random.Word())
|
||||||
.RuleFor(m => m.DisplayOrder, f => f.Random.Number(1, 100))
|
.RuleFor(m => m.DisplayOrder, f => f.Random.Number(1, 100))
|
||||||
.RuleFor(m => m.ParentId, f => f.Random.Number(0, 10)) // 假设可以有父菜单
|
.RuleFor(m => m.ParentId, f => f.Random.Number(0, 10)) // 假设可以有父菜单
|
||||||
.RuleFor(m => m.MenuType, f => f.PickRandom<MenuType>()) // 假设 MenuType 是一个枚举
|
|
||||||
.Generate();
|
.Generate();
|
||||||
return menuDto;
|
return menuDto;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,6 +41,10 @@ public class DbMenu
|
|||||||
/// 菜单关联的数据ID,例如设备Id,变量表Id
|
/// 菜单关联的数据ID,例如设备Id,变量表Id
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int TargetId { get; set; }
|
public int TargetId { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 菜单关联的数据ID,例如设备Id,变量表Id
|
||||||
|
/// </summary>
|
||||||
|
public string TargetViewKey { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 导航的可选参数
|
/// 导航的可选参数
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ public class InitializeRepository : IInitializeRepository
|
|||||||
public InitializeRepository(SqlSugarDbContext dbContext)
|
public InitializeRepository(SqlSugarDbContext dbContext)
|
||||||
{
|
{
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
_db = _dbContext.GetInstance();
|
_db = _dbContext.GetInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -44,7 +44,7 @@ public class InitializeRepository : IInitializeRepository
|
|||||||
_db.CodeFirst.InitTables<DbVariableMqttAlias>();
|
_db.CodeFirst.InitTables<DbVariableMqttAlias>();
|
||||||
_db.CodeFirst.InitTables<DbMenu>();
|
_db.CodeFirst.InitTables<DbMenu>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 初始化数据库表索引。
|
/// 初始化数据库表索引。
|
||||||
/// 为特定表的列创建唯一索引,以提高查询效率和数据完整性。
|
/// 为特定表的列创建唯一索引,以提高查询效率和数据完整性。
|
||||||
@@ -52,22 +52,22 @@ public class InitializeRepository : IInitializeRepository
|
|||||||
public void InitializeTableIndex()
|
public void InitializeTableIndex()
|
||||||
{
|
{
|
||||||
// 为 DbDevice 表创建索引
|
// 为 DbDevice 表创建索引
|
||||||
_db.DbMaintenance.CreateIndex(nameof(DbDevice),new []
|
_db.DbMaintenance.CreateIndex(nameof(DbDevice), new[]
|
||||||
{
|
{
|
||||||
nameof(DbDevice.Name),
|
nameof(DbDevice.Name),
|
||||||
nameof(DbDevice.OpcUaServerUrl),
|
nameof(DbDevice.OpcUaServerUrl),
|
||||||
},true);
|
}, true);
|
||||||
|
|
||||||
// 为 DbVariable 表创建索引
|
// 为 DbVariable 表创建索引
|
||||||
_db.DbMaintenance.CreateIndex(nameof(DbVariable),new []
|
_db.DbMaintenance.CreateIndex(nameof(DbVariable), new[]
|
||||||
{
|
{
|
||||||
nameof(DbVariable.OpcUaNodeId)
|
nameof(DbVariable.OpcUaNodeId)
|
||||||
},true);
|
}, true);
|
||||||
// 为 DbMqttServer 表创建索引
|
// 为 DbMqttServer 表创建索引
|
||||||
_db.DbMaintenance.CreateIndex(nameof(DbMqttServer),new []
|
_db.DbMaintenance.CreateIndex(nameof(DbMqttServer), new[]
|
||||||
{
|
{
|
||||||
nameof(DbMqttServer.ServerName)
|
nameof(DbMqttServer.ServerName)
|
||||||
},true);
|
}, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -77,7 +77,7 @@ public class InitializeRepository : IInitializeRepository
|
|||||||
/// <returns>如果表存在则为 true,否则为 false。</returns>
|
/// <returns>如果表存在则为 true,否则为 false。</returns>
|
||||||
public bool IsAnyTable(string tableName)
|
public bool IsAnyTable(string tableName)
|
||||||
{
|
{
|
||||||
return _db.DbMaintenance.IsAnyTable(tableName, false);
|
return _db.DbMaintenance.IsAnyTable(tableName, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -87,7 +87,7 @@ public class InitializeRepository : IInitializeRepository
|
|||||||
/// <returns>如果索引存在则为 true,否则为 false。</returns>
|
/// <returns>如果索引存在则为 true,否则为 false。</returns>
|
||||||
public bool IsAnyIndex(string indexName)
|
public bool IsAnyIndex(string indexName)
|
||||||
{
|
{
|
||||||
return _db.DbMaintenance.IsAnyIndex(indexName);
|
return _db.DbMaintenance.IsAnyIndex(indexName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -97,23 +97,53 @@ public class InitializeRepository : IInitializeRepository
|
|||||||
public void InitializeMenus()
|
public void InitializeMenus()
|
||||||
{
|
{
|
||||||
// 检查数据库中是否已存在菜单数据
|
// 检查数据库中是否已存在菜单数据
|
||||||
if (_db.Queryable<DbMenu>().Any())
|
if (_db.Queryable<DbMenu>()
|
||||||
|
.Any())
|
||||||
{
|
{
|
||||||
return; // 如果数据库中已经有菜单,则不进行初始化
|
return; // 如果数据库中已经有菜单,则不进行初始化
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建默认菜单项的 DbMenu 实体列表
|
// 创建默认菜单项的 DbMenu 实体列表
|
||||||
var defaultMenus = new List<DbMenu>
|
var defaultMenus = new List<DbMenu>
|
||||||
{
|
{
|
||||||
new DbMenu { Id = 1, Header = "主页", Icon = "\uE80F", ParentId = 0,MenuType = MenuType.MainMenu, DisplayOrder = 1 },
|
new DbMenu
|
||||||
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 },
|
Id = 1, Header = "主页", Icon = "\uE80F", ParentId = 0,
|
||||||
new DbMenu { Id = 4, Header = "Mqtt服务器", Icon = "\uE753", ParentId = 0,MenuType = MenuType.MainMenu , DisplayOrder = 4 },
|
MenuType = MenuType.MainMenu, TargetViewKey = "HomeView", DisplayOrder = 1
|
||||||
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 = 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -56,6 +56,7 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
|
|||||||
|
|
||||||
public ConcurrentDictionary<int, Variable> AllVariables;
|
public ConcurrentDictionary<int, Variable> AllVariables;
|
||||||
private readonly IMenuService _menuService;
|
private readonly IMenuService _menuService;
|
||||||
|
private readonly INavigationService _navigationService;
|
||||||
|
|
||||||
|
|
||||||
// 设备列表变更事件,当设备列表数据更新时触发。
|
// 设备列表变更事件,当设备列表数据更新时触发。
|
||||||
@@ -79,13 +80,14 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
|
|||||||
/// <param name="mapper">AutoMapper 实例。</param>
|
/// <param name="mapper">AutoMapper 实例。</param>
|
||||||
/// <param name="varDataRepository"></param>
|
/// <param name="varDataRepository"></param>
|
||||||
public DataServices(IMapper mapper, IDeviceAppService deviceAppService,
|
public DataServices(IMapper mapper, IDeviceAppService deviceAppService,
|
||||||
IVariableTableAppService variableTableAppService, IVariableAppService variableAppService,IMenuService menuService)
|
IVariableTableAppService variableTableAppService, IVariableAppService variableAppService,IMenuService menuService,INavigationService navigationService)
|
||||||
{
|
{
|
||||||
_mapper = mapper;
|
_mapper = mapper;
|
||||||
_deviceAppService = deviceAppService;
|
_deviceAppService = deviceAppService;
|
||||||
_variableTableAppService = variableTableAppService;
|
_variableTableAppService = variableTableAppService;
|
||||||
_variableAppService = variableAppService;
|
_variableAppService = variableAppService;
|
||||||
_menuService = menuService;
|
_menuService = menuService;
|
||||||
|
_navigationService = navigationService;
|
||||||
IsActive = true; // 激活消息接收器
|
IsActive = true; // 激活消息接收器
|
||||||
Devices = new ObservableCollection<DeviceItemViewModel>();
|
Devices = new ObservableCollection<DeviceItemViewModel>();
|
||||||
VariableTables = new ObservableCollection<VariableTableItemViewModel>();
|
VariableTables = new ObservableCollection<VariableTableItemViewModel>();
|
||||||
@@ -246,7 +248,7 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
|
|||||||
{
|
{
|
||||||
// 这是一个新菜单项,添加到集合中
|
// 这是一个新菜单项,添加到集合中
|
||||||
// 注意:这里直接添加 IMenuService 返回的 MenuItemViewModel 实例
|
// 注意:这里直接添加 IMenuService 返回的 MenuItemViewModel 实例
|
||||||
Menus.Add(_mapper.Map<MenuBeanItemViewModel>(newDto));
|
Menus.Add(new MenuBeanItemViewModel(newDto,_navigationService));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
|
using System.Windows.Input;
|
||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
using CommunityToolkit.Mvvm.Input;
|
||||||
using DMS.Application.DTOs;
|
using DMS.Application.DTOs;
|
||||||
using DMS.Core.Enums;
|
using DMS.Core.Enums;
|
||||||
|
using DMS.WPF.Services;
|
||||||
|
|
||||||
namespace DMS.WPF.ViewModels.Items;
|
namespace DMS.WPF.ViewModels.Items;
|
||||||
|
|
||||||
@@ -23,6 +26,8 @@ public partial class MenuBeanItemViewModel : ObservableObject
|
|||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
private int _targetId;
|
private int _targetId;
|
||||||
|
[ObservableProperty]
|
||||||
|
private string _targetViewKey;
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
private string _navigationParameter;
|
private string _navigationParameter;
|
||||||
@@ -31,16 +36,26 @@ public partial class MenuBeanItemViewModel : ObservableObject
|
|||||||
private int _displayOrder;
|
private int _displayOrder;
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
private ObservableCollection<MenuBeanItemViewModel> _children=new ();
|
private ObservableCollection<MenuBeanItemViewModel> _children=new ();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 菜单项点击时执行的导航命令。
|
||||||
|
/// </summary>
|
||||||
|
public ICommand NavigateCommand { get; }
|
||||||
|
|
||||||
public MenuBeanItemViewModel(MenuBeanDto dto)
|
public MenuBeanItemViewModel(MenuBeanDto dto,INavigationService navigationService)
|
||||||
{
|
{
|
||||||
Id = dto.Id;
|
Id = dto.Id;
|
||||||
_parentId = dto.ParentId;
|
_parentId = dto.ParentId;
|
||||||
_header = dto.Header;
|
_header = dto.Header;
|
||||||
_icon = dto.Icon;
|
_icon = dto.Icon;
|
||||||
_menuType = dto.MenuType;
|
_menuType = dto.MenuType;
|
||||||
|
_targetViewKey=dto.TargetViewKey;
|
||||||
_targetId = dto.TargetId;
|
_targetId = dto.TargetId;
|
||||||
_navigationParameter = dto.NavigationParameter;
|
_navigationParameter = dto.NavigationParameter;
|
||||||
_displayOrder = dto.DisplayOrder;
|
_displayOrder = dto.DisplayOrder;
|
||||||
|
NavigateCommand = new AsyncRelayCommand(async () =>
|
||||||
|
{
|
||||||
|
await navigationService.NavigateToAsync(_targetViewKey, _navigationParameter);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,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}"
|
||||||
|
|
||||||
MenuItemsSource="{Binding Children }">
|
MenuItemsSource="{Binding Children }">
|
||||||
<ui:NavigationViewItem.Icon>
|
<ui:NavigationViewItem.Icon>
|
||||||
<ui:FontIcon Glyph="{Binding Icon}" />
|
<ui:FontIcon Glyph="{Binding Icon}" />
|
||||||
|
|||||||
Reference in New Issue
Block a user