给Menu菜单绑定对应的数据
This commit is contained in:
@@ -69,7 +69,10 @@ public partial class App : Application
|
||||
InitMenu().Await((e) =>
|
||||
{
|
||||
NotificationHelper.ShowMessage($"初始化主菜单失败:{e.Message}");
|
||||
}, () => { });
|
||||
}, () =>
|
||||
{
|
||||
MessageHelper.SendLoadMessage(LoadTypes.Menu);
|
||||
});
|
||||
|
||||
MainWindow = Services.GetRequiredService<MainView>();
|
||||
MainWindow.Show();
|
||||
|
||||
@@ -14,7 +14,7 @@ public class DeviceRepository
|
||||
{
|
||||
_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();
|
||||
if (exist != null)
|
||||
@@ -28,7 +28,8 @@ public class DeviceRepository
|
||||
dbVariableTable.Description = "默认变量表";
|
||||
dbVariableTable.ProtocolType = dbDevice.ProtocolType;
|
||||
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()
|
||||
|
||||
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,
|
||||
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 ProtocolType ProtocolType { get; set; }
|
||||
public List<DataVariable> DataVariables { get; set; }
|
||||
public Device? Device { get; set; }
|
||||
}
|
||||
@@ -10,13 +10,12 @@ using PMSWPF.Models;
|
||||
|
||||
namespace PMSWPF.Services;
|
||||
|
||||
public partial class DataServices:ObservableRecipient,IRecipient<LoadMessage>
|
||||
public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
|
||||
{
|
||||
private readonly ILogger<DataServices> _logger;
|
||||
[ObservableProperty]
|
||||
private List<Device> _devices = new List<Device>();
|
||||
[ObservableProperty]
|
||||
private List<MenuBean> menuBeans = new List<MenuBean>();
|
||||
[ObservableProperty] private List<Device> _devices = new List<Device>();
|
||||
[ObservableProperty] private List<VariableTable> _variableTables = new ();
|
||||
[ObservableProperty] private List<MenuBean> menuBeans = new List<MenuBean>();
|
||||
private readonly DeviceRepository _deviceRepository;
|
||||
private readonly MenuRepository _menuRepository;
|
||||
|
||||
@@ -24,53 +23,100 @@ public partial class DataServices:ObservableRecipient,IRecipient<LoadMessage>
|
||||
public event Action<List<MenuBean>> OnMenuListChanged;
|
||||
|
||||
|
||||
partial void OnDevicesChanged(List<Device> devices)
|
||||
partial void OnDevicesChanged(List<Device> devices)
|
||||
{
|
||||
OnDeviceListChanged?.Invoke(devices);
|
||||
FillMenuData(MenuBeans);
|
||||
}
|
||||
|
||||
partial void OnMenuBeansChanged(List<MenuBean> menuBeans)
|
||||
{
|
||||
OnMenuListChanged?.Invoke(menuBeans);
|
||||
}
|
||||
|
||||
|
||||
public DataServices(ILogger<DataServices> logger )
|
||||
|
||||
public DataServices(ILogger<DataServices> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
IsActive = true;
|
||||
_deviceRepository = new DeviceRepository();
|
||||
_menuRepository = new MenuRepository();
|
||||
_deviceRepository = new DeviceRepository();
|
||||
_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)
|
||||
{
|
||||
|
||||
if (!(message.Value is LoadTypes))
|
||||
throw new ArgumentException($"接受到的加载类型错误:{message.Value}");
|
||||
try
|
||||
{
|
||||
|
||||
switch ((LoadTypes)message.Value )
|
||||
switch ((LoadTypes)message.Value)
|
||||
{
|
||||
case LoadTypes.All:
|
||||
Devices = await _deviceRepository.GetAll();
|
||||
MenuBeans = await _menuRepository.GetMenu();
|
||||
break;
|
||||
case LoadTypes.Devices:
|
||||
Devices= await _deviceRepository.GetAll();
|
||||
Devices = await _deviceRepository.GetAll();
|
||||
break;
|
||||
case LoadTypes.Menu:
|
||||
MenuBeans= await _menuRepository.GetMenu();
|
||||
MenuBeans = await _menuRepository.GetMenu();
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
NotificationHelper.ShowMessage($"加载数据出现了错误:{e.Message}");
|
||||
_logger.LogError($"加载数据出现了错误:{e.Message}");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -33,8 +33,8 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
_logger = logger;
|
||||
_dialogService = dialogService;
|
||||
_dataServices = dataServices;
|
||||
|
||||
WeakReferenceMessenger.Default.Send<LoadMessage>(new LoadMessage(LoadTypes.Devices));
|
||||
|
||||
MessageHelper.SendLoadMessage(LoadTypes.Devices);
|
||||
_dataServices.OnDeviceListChanged += (devices) => { Devices = new ObservableCollection<Device>(devices); };
|
||||
}
|
||||
|
||||
@@ -51,7 +51,8 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
device = await _dialogService.ShowAddDeviceDialog();
|
||||
if (device != null)
|
||||
{
|
||||
if (await _deviceRepository.Add(device))
|
||||
device= await _deviceRepository.Add(device);
|
||||
if (device!=null)
|
||||
{
|
||||
var msg = $"添加设备成功:{device.Name}";
|
||||
_logger.LogInformation(msg);
|
||||
@@ -60,7 +61,7 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
if (addMenuRes)
|
||||
{
|
||||
// 通知更新菜单
|
||||
WeakReferenceMessenger.Default.Send<UpdateMenuMessage>(new UpdateMenuMessage(0));
|
||||
MessageHelper.SendLoadMessage(LoadTypes.Menu);
|
||||
NotificationHelper.ShowMessage(msg, NotificationType.Success);
|
||||
}
|
||||
else
|
||||
@@ -82,7 +83,7 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
{
|
||||
var msg = $"添加设备失败:{e.Message}";
|
||||
_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.Repositories;
|
||||
using PMSWPF.Enums;
|
||||
using PMSWPF.Helper;
|
||||
using PMSWPF.Message;
|
||||
using PMSWPF.Models;
|
||||
using PMSWPF.Services;
|
||||
@@ -28,10 +29,12 @@ public partial class MainViewModel : ViewModelBase
|
||||
_dataServices = dataServices;
|
||||
|
||||
_navgatorServices.OnViewModelChanged += () => { CurrentViewModel = _navgatorServices.CurrentViewModel; };
|
||||
|
||||
CurrentViewModel = new HomeViewModel();
|
||||
CurrentViewModel.OnLoaded();
|
||||
|
||||
WeakReferenceMessenger.Default.Send<LoadMessage>(new LoadMessage(LoadTypes.Menu));
|
||||
// 发送消息加载数据
|
||||
MessageHelper.SendLoadMessage(LoadTypes.All);
|
||||
// 当菜单加载成功后,在前台显示菜单
|
||||
dataServices.OnMenuListChanged += (menus) =>
|
||||
{
|
||||
Menus = new ObservableCollection<MenuBean>(menus);
|
||||
|
||||
@@ -4,6 +4,7 @@ using CommunityToolkit.Mvvm.Messaging;
|
||||
using iNKORE.UI.WPF.Modern.Controls;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PMSWPF.Helper;
|
||||
using PMSWPF.Message;
|
||||
using PMSWPF.Models;
|
||||
using PMSWPF.ViewModels;
|
||||
@@ -58,9 +59,8 @@ public partial class MainView : Window
|
||||
_logger.LogInformation("导航到到设备页面");
|
||||
break;
|
||||
}
|
||||
|
||||
var nm = new NavgatorMessage(navgateVM);
|
||||
WeakReferenceMessenger.Default.Send(nm);
|
||||
|
||||
MessageHelper.SendNavgatorMessage(navgateVM);
|
||||
}
|
||||
|
||||
private async void MainView_OnLoaded(object sender, RoutedEventArgs e)
|
||||
@@ -95,9 +95,8 @@ public partial class MainView : Window
|
||||
_logger.LogInformation("导航到到设备页面");
|
||||
break;
|
||||
}
|
||||
|
||||
var nm = new NavgatorMessage(navgateVM,parameter);
|
||||
WeakReferenceMessenger.Default.Send(nm);
|
||||
|
||||
MessageHelper.SendNavgatorMessage(navgateVM,parameter);
|
||||
}
|
||||
|
||||
private void NavigationView_OnSelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
|
||||
|
||||
Reference in New Issue
Block a user