给Menu菜单绑定对应的数据
This commit is contained in:
@@ -69,7 +69,10 @@ public partial class App : Application
|
|||||||
InitMenu().Await((e) =>
|
InitMenu().Await((e) =>
|
||||||
{
|
{
|
||||||
NotificationHelper.ShowMessage($"初始化主菜单失败:{e.Message}");
|
NotificationHelper.ShowMessage($"初始化主菜单失败:{e.Message}");
|
||||||
}, () => { });
|
}, () =>
|
||||||
|
{
|
||||||
|
MessageHelper.SendLoadMessage(LoadTypes.Menu);
|
||||||
|
});
|
||||||
|
|
||||||
MainWindow = Services.GetRequiredService<MainView>();
|
MainWindow = Services.GetRequiredService<MainView>();
|
||||||
MainWindow.Show();
|
MainWindow.Show();
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ public class DeviceRepository
|
|||||||
{
|
{
|
||||||
_db = DbContext.GetInstance();
|
_db = DbContext.GetInstance();
|
||||||
}
|
}
|
||||||
public async Task<bool> Add(Device device)
|
public async Task<Device> Add(Device device)
|
||||||
{
|
{
|
||||||
var exist = await _db.Queryable<DbDevice>().Where(d => d.Name == device.Name).FirstAsync();
|
var exist = await _db.Queryable<DbDevice>().Where(d => d.Name == device.Name).FirstAsync();
|
||||||
if (exist != null)
|
if (exist != null)
|
||||||
@@ -28,7 +28,8 @@ public class DeviceRepository
|
|||||||
dbVariableTable.Description = "默认变量表";
|
dbVariableTable.Description = "默认变量表";
|
||||||
dbVariableTable.ProtocolType = dbDevice.ProtocolType;
|
dbVariableTable.ProtocolType = dbDevice.ProtocolType;
|
||||||
dbDevice.VariableTables.Add(dbVariableTable);
|
dbDevice.VariableTables.Add(dbVariableTable);
|
||||||
return await _db.InsertNav(dbDevice).Include(d => d.VariableTables).ExecuteCommandAsync();
|
var addDbDevice= await _db.InsertNav(dbDevice).Include(d => d.VariableTables).ExecuteReturnEntityAsync();
|
||||||
|
return addDbDevice.CopyTo<Device>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<Device>> GetAll()
|
public async Task<List<Device>> GetAll()
|
||||||
|
|||||||
53
Data/Repositories/VariableTableRepository.cs
Normal file
53
Data/Repositories/VariableTableRepository.cs
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
using PMSWPF.Data.Entities;
|
||||||
|
using PMSWPF.Extensions;
|
||||||
|
using PMSWPF.Models;
|
||||||
|
using SqlSugar;
|
||||||
|
|
||||||
|
namespace PMSWPF.Data.Repositories;
|
||||||
|
|
||||||
|
public class VariableTableRepository
|
||||||
|
{
|
||||||
|
private SqlSugarClient _db;
|
||||||
|
public VariableTableRepository()
|
||||||
|
{
|
||||||
|
_db = DbContext.GetInstance();
|
||||||
|
}
|
||||||
|
// public async Task<VariableTable> Add(VariableTable variableTable)
|
||||||
|
// {
|
||||||
|
// var exist = await _db.Queryable<DbDevice>().Where(d => d.Name == device.Name).FirstAsync();
|
||||||
|
// if (exist != null)
|
||||||
|
// throw new InvalidOperationException("设备名称已经存在。");
|
||||||
|
// var dbDevice = new DbDevice();
|
||||||
|
// device.CopyTo(dbDevice);
|
||||||
|
// dbDevice.VariableTables = new List<DbVariableTable>();
|
||||||
|
// // 添加默认变量表
|
||||||
|
// var dbVariableTable = new DbVariableTable();
|
||||||
|
// dbVariableTable.Name = "默认变量表";
|
||||||
|
// dbVariableTable.Description = "默认变量表";
|
||||||
|
// dbVariableTable.ProtocolType = dbDevice.ProtocolType;
|
||||||
|
// dbDevice.VariableTables.Add(dbVariableTable);
|
||||||
|
// var addDbDevice= await _db.InsertNav(dbDevice).Include(d => d.VariableTables).ExecuteReturnEntityAsync();
|
||||||
|
// return addDbDevice.CopyTo<Device>();
|
||||||
|
// }
|
||||||
|
|
||||||
|
public async Task<List<VariableTable>> GetAll()
|
||||||
|
{
|
||||||
|
var dbVariableTables = await _db.Queryable<DbVariableTable>().ToListAsync();
|
||||||
|
var variableTables = new List<VariableTable>();
|
||||||
|
|
||||||
|
foreach (var dbVariableTable in dbVariableTables)
|
||||||
|
variableTables.Add(dbVariableTable.CopyTo<VariableTable>());
|
||||||
|
|
||||||
|
return variableTables;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<DbVariableTable> GetById(int id)
|
||||||
|
{
|
||||||
|
return await _db.Queryable<DbVariableTable>().FirstAsync(p => p.Id == id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<int> DeleteById(int id)
|
||||||
|
{
|
||||||
|
return await _db.Deleteable<DbVariableTable>(new DbVariableTable { Id = id }).ExecuteCommandAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,5 +4,6 @@ public enum LoadTypes
|
|||||||
{
|
{
|
||||||
Devices,
|
Devices,
|
||||||
Menu,
|
Menu,
|
||||||
Mqtt
|
Mqtt,
|
||||||
|
All
|
||||||
}
|
}
|
||||||
34
Helper/MessageHelper.cs
Normal file
34
Helper/MessageHelper.cs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
using CommunityToolkit.Mvvm.Messaging;
|
||||||
|
using PMSWPF.Enums;
|
||||||
|
using PMSWPF.Message;
|
||||||
|
using PMSWPF.ViewModels;
|
||||||
|
|
||||||
|
namespace PMSWPF.Helper;
|
||||||
|
|
||||||
|
public class MessageHelper
|
||||||
|
{
|
||||||
|
public static void Send<T>(T message) where T : class
|
||||||
|
{
|
||||||
|
WeakReferenceMessenger.Default.Send<T>(message);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 发送加载消息
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="loadType">加载的类型,如菜单</param>
|
||||||
|
public static void SendLoadMessage(LoadTypes loadType)
|
||||||
|
{
|
||||||
|
WeakReferenceMessenger.Default.Send<LoadMessage>(new LoadMessage(loadType));
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 发送导航消息
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="vm">导航View的ViewModel</param>
|
||||||
|
/// <param name="param">带的参数</param>
|
||||||
|
public static void SendNavgatorMessage(ViewModelBase vm,Object param=null)
|
||||||
|
{
|
||||||
|
WeakReferenceMessenger.Default.Send<NavgatorMessage>(new NavgatorMessage(vm)
|
||||||
|
{
|
||||||
|
Parameters = param
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,4 +9,5 @@ public class VariableTable
|
|||||||
public string Description { get; set; }
|
public string Description { get; set; }
|
||||||
public ProtocolType ProtocolType { get; set; }
|
public ProtocolType ProtocolType { get; set; }
|
||||||
public List<DataVariable> DataVariables { get; set; }
|
public List<DataVariable> DataVariables { get; set; }
|
||||||
|
public Device? Device { get; set; }
|
||||||
}
|
}
|
||||||
@@ -10,13 +10,12 @@ using PMSWPF.Models;
|
|||||||
|
|
||||||
namespace PMSWPF.Services;
|
namespace PMSWPF.Services;
|
||||||
|
|
||||||
public partial class DataServices:ObservableRecipient,IRecipient<LoadMessage>
|
public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
|
||||||
{
|
{
|
||||||
private readonly ILogger<DataServices> _logger;
|
private readonly ILogger<DataServices> _logger;
|
||||||
[ObservableProperty]
|
[ObservableProperty] private List<Device> _devices = new List<Device>();
|
||||||
private List<Device> _devices = new List<Device>();
|
[ObservableProperty] private List<VariableTable> _variableTables = new ();
|
||||||
[ObservableProperty]
|
[ObservableProperty] private List<MenuBean> menuBeans = new List<MenuBean>();
|
||||||
private List<MenuBean> menuBeans = new List<MenuBean>();
|
|
||||||
private readonly DeviceRepository _deviceRepository;
|
private readonly DeviceRepository _deviceRepository;
|
||||||
private readonly MenuRepository _menuRepository;
|
private readonly MenuRepository _menuRepository;
|
||||||
|
|
||||||
@@ -27,6 +26,7 @@ public partial class DataServices:ObservableRecipient,IRecipient<LoadMessage>
|
|||||||
partial void OnDevicesChanged(List<Device> devices)
|
partial void OnDevicesChanged(List<Device> devices)
|
||||||
{
|
{
|
||||||
OnDeviceListChanged?.Invoke(devices);
|
OnDeviceListChanged?.Invoke(devices);
|
||||||
|
FillMenuData(MenuBeans);
|
||||||
}
|
}
|
||||||
|
|
||||||
partial void OnMenuBeansChanged(List<MenuBean> menuBeans)
|
partial void OnMenuBeansChanged(List<MenuBean> menuBeans)
|
||||||
@@ -35,42 +35,88 @@ public partial class DataServices:ObservableRecipient,IRecipient<LoadMessage>
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public DataServices(ILogger<DataServices> logger )
|
public DataServices(ILogger<DataServices> logger)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
IsActive = true;
|
IsActive = true;
|
||||||
_deviceRepository = new DeviceRepository();
|
_deviceRepository = new DeviceRepository();
|
||||||
_menuRepository = new MenuRepository();
|
_menuRepository = new MenuRepository();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 给Menu菜单的Data填充数据
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="menuBeans"></param>
|
||||||
|
private void FillMenuData(List<MenuBean> menuBeans)
|
||||||
|
{
|
||||||
|
if (menuBeans == null || menuBeans.Count == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
foreach (MenuBean menuBean in menuBeans)
|
||||||
|
{
|
||||||
|
switch (menuBean.Type)
|
||||||
|
{
|
||||||
|
case MenuType.MainMenu:
|
||||||
|
break;
|
||||||
|
case MenuType.DeviceMenu:
|
||||||
|
menuBean.Data= Devices.FirstOrDefault(d => d.Id == menuBean.DataId);
|
||||||
|
break;
|
||||||
|
case MenuType.VariableTableMenu:
|
||||||
|
menuBean.Data= FindVarTableForDevice(menuBean.DataId);
|
||||||
|
// menuBean.Data= Devices.FirstOrDefault(d => d.Id == menuBean.DataId);
|
||||||
|
break;
|
||||||
|
case MenuType.AddVariableTableMenu:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (menuBean.Items!=null && menuBean.Items.Count>0)
|
||||||
|
{
|
||||||
|
FillMenuData(menuBean.Items);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private VariableTable FindVarTableForDevice(int vtableId)
|
||||||
|
{
|
||||||
|
VariableTable varTable = null;
|
||||||
|
foreach (var device in _devices)
|
||||||
|
{
|
||||||
|
varTable= device.VariableTables.FirstOrDefault(v => v.Id == vtableId);
|
||||||
|
return varTable;
|
||||||
|
}
|
||||||
|
return varTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public async void Receive(LoadMessage message)
|
public async void Receive(LoadMessage message)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (!(message.Value is LoadTypes))
|
if (!(message.Value is LoadTypes))
|
||||||
throw new ArgumentException($"接受到的加载类型错误:{message.Value}");
|
throw new ArgumentException($"接受到的加载类型错误:{message.Value}");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
switch ((LoadTypes)message.Value)
|
||||||
switch ((LoadTypes)message.Value )
|
|
||||||
{
|
{
|
||||||
|
case LoadTypes.All:
|
||||||
|
Devices = await _deviceRepository.GetAll();
|
||||||
|
MenuBeans = await _menuRepository.GetMenu();
|
||||||
|
break;
|
||||||
case LoadTypes.Devices:
|
case LoadTypes.Devices:
|
||||||
Devices= await _deviceRepository.GetAll();
|
Devices = await _deviceRepository.GetAll();
|
||||||
break;
|
break;
|
||||||
case LoadTypes.Menu:
|
case LoadTypes.Menu:
|
||||||
MenuBeans= await _menuRepository.GetMenu();
|
MenuBeans = await _menuRepository.GetMenu();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
NotificationHelper.ShowMessage($"加载数据出现了错误:{e.Message}");
|
NotificationHelper.ShowMessage($"加载数据出现了错误:{e.Message}");
|
||||||
_logger.LogError($"加载数据出现了错误:{e.Message}");
|
_logger.LogError($"加载数据出现了错误:{e.Message}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -34,7 +34,7 @@ public partial class DevicesViewModel : ViewModelBase
|
|||||||
_dialogService = dialogService;
|
_dialogService = dialogService;
|
||||||
_dataServices = dataServices;
|
_dataServices = dataServices;
|
||||||
|
|
||||||
WeakReferenceMessenger.Default.Send<LoadMessage>(new LoadMessage(LoadTypes.Devices));
|
MessageHelper.SendLoadMessage(LoadTypes.Devices);
|
||||||
_dataServices.OnDeviceListChanged += (devices) => { Devices = new ObservableCollection<Device>(devices); };
|
_dataServices.OnDeviceListChanged += (devices) => { Devices = new ObservableCollection<Device>(devices); };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,7 +51,8 @@ public partial class DevicesViewModel : ViewModelBase
|
|||||||
device = await _dialogService.ShowAddDeviceDialog();
|
device = await _dialogService.ShowAddDeviceDialog();
|
||||||
if (device != null)
|
if (device != null)
|
||||||
{
|
{
|
||||||
if (await _deviceRepository.Add(device))
|
device= await _deviceRepository.Add(device);
|
||||||
|
if (device!=null)
|
||||||
{
|
{
|
||||||
var msg = $"添加设备成功:{device.Name}";
|
var msg = $"添加设备成功:{device.Name}";
|
||||||
_logger.LogInformation(msg);
|
_logger.LogInformation(msg);
|
||||||
@@ -60,7 +61,7 @@ public partial class DevicesViewModel : ViewModelBase
|
|||||||
if (addMenuRes)
|
if (addMenuRes)
|
||||||
{
|
{
|
||||||
// 通知更新菜单
|
// 通知更新菜单
|
||||||
WeakReferenceMessenger.Default.Send<UpdateMenuMessage>(new UpdateMenuMessage(0));
|
MessageHelper.SendLoadMessage(LoadTypes.Menu);
|
||||||
NotificationHelper.ShowMessage(msg, NotificationType.Success);
|
NotificationHelper.ShowMessage(msg, NotificationType.Success);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -82,7 +83,7 @@ public partial class DevicesViewModel : ViewModelBase
|
|||||||
{
|
{
|
||||||
var msg = $"添加设备失败:{e.Message}";
|
var msg = $"添加设备失败:{e.Message}";
|
||||||
_logger.LogError(msg);
|
_logger.LogError(msg);
|
||||||
NotificationHelper.ShowMessage(msg, NotificationType.Success);
|
NotificationHelper.ShowMessage(msg, NotificationType.Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using CommunityToolkit.Mvvm.Messaging;
|
|||||||
using PMSWPF.Data.Entities;
|
using PMSWPF.Data.Entities;
|
||||||
using PMSWPF.Data.Repositories;
|
using PMSWPF.Data.Repositories;
|
||||||
using PMSWPF.Enums;
|
using PMSWPF.Enums;
|
||||||
|
using PMSWPF.Helper;
|
||||||
using PMSWPF.Message;
|
using PMSWPF.Message;
|
||||||
using PMSWPF.Models;
|
using PMSWPF.Models;
|
||||||
using PMSWPF.Services;
|
using PMSWPF.Services;
|
||||||
@@ -28,10 +29,12 @@ public partial class MainViewModel : ViewModelBase
|
|||||||
_dataServices = dataServices;
|
_dataServices = dataServices;
|
||||||
|
|
||||||
_navgatorServices.OnViewModelChanged += () => { CurrentViewModel = _navgatorServices.CurrentViewModel; };
|
_navgatorServices.OnViewModelChanged += () => { CurrentViewModel = _navgatorServices.CurrentViewModel; };
|
||||||
|
|
||||||
CurrentViewModel = new HomeViewModel();
|
CurrentViewModel = new HomeViewModel();
|
||||||
CurrentViewModel.OnLoaded();
|
CurrentViewModel.OnLoaded();
|
||||||
|
// 发送消息加载数据
|
||||||
WeakReferenceMessenger.Default.Send<LoadMessage>(new LoadMessage(LoadTypes.Menu));
|
MessageHelper.SendLoadMessage(LoadTypes.All);
|
||||||
|
// 当菜单加载成功后,在前台显示菜单
|
||||||
dataServices.OnMenuListChanged += (menus) =>
|
dataServices.OnMenuListChanged += (menus) =>
|
||||||
{
|
{
|
||||||
Menus = new ObservableCollection<MenuBean>(menus);
|
Menus = new ObservableCollection<MenuBean>(menus);
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using CommunityToolkit.Mvvm.Messaging;
|
|||||||
using iNKORE.UI.WPF.Modern.Controls;
|
using iNKORE.UI.WPF.Modern.Controls;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using PMSWPF.Helper;
|
||||||
using PMSWPF.Message;
|
using PMSWPF.Message;
|
||||||
using PMSWPF.Models;
|
using PMSWPF.Models;
|
||||||
using PMSWPF.ViewModels;
|
using PMSWPF.ViewModels;
|
||||||
@@ -59,8 +60,7 @@ public partial class MainView : Window
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
var nm = new NavgatorMessage(navgateVM);
|
MessageHelper.SendNavgatorMessage(navgateVM);
|
||||||
WeakReferenceMessenger.Default.Send(nm);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void MainView_OnLoaded(object sender, RoutedEventArgs e)
|
private async void MainView_OnLoaded(object sender, RoutedEventArgs e)
|
||||||
@@ -96,8 +96,7 @@ public partial class MainView : Window
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
var nm = new NavgatorMessage(navgateVM,parameter);
|
MessageHelper.SendNavgatorMessage(navgateVM,parameter);
|
||||||
WeakReferenceMessenger.Default.Send(nm);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void NavigationView_OnSelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
|
private void NavigationView_OnSelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
|
||||||
|
|||||||
Reference in New Issue
Block a user