修改了Dialog的调用方式,和实现了添加设备添加到侧边菜单中
This commit is contained in:
@@ -16,23 +16,26 @@ namespace PMSWPF.ViewModels;
|
||||
|
||||
public partial class DevicesViewModel : ViewModelBase
|
||||
{
|
||||
private readonly DevicesRepositories _devicesRepositories;
|
||||
private readonly DeviceRepository _deviceRepository;
|
||||
private readonly ILogger<DevicesViewModel> _logger;
|
||||
private readonly IDialogService _dialogService;
|
||||
|
||||
[ObservableProperty] private ObservableCollection<Device> _devices;
|
||||
private readonly MenuRepository _menuRepository;
|
||||
|
||||
public DevicesViewModel(DevicesRepositories devicesRepositories,
|
||||
ILogger<DevicesViewModel> logger
|
||||
public DevicesViewModel(
|
||||
ILogger<DevicesViewModel> logger, IDialogService dialogService
|
||||
)
|
||||
{
|
||||
|
||||
_devicesRepositories = devicesRepositories;
|
||||
_deviceRepository = new DeviceRepository();
|
||||
_menuRepository = new MenuRepository();
|
||||
_logger = logger;
|
||||
_dialogService = dialogService;
|
||||
}
|
||||
|
||||
public async Task OnLoadedAsync()
|
||||
{
|
||||
var ds = await _devicesRepositories.GetAll();
|
||||
var ds = await _deviceRepository.GetAll();
|
||||
Devices = new ObservableCollection<Device>(ds);
|
||||
}
|
||||
|
||||
@@ -42,22 +45,48 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
Device device = null;
|
||||
try
|
||||
{
|
||||
OpenDialogMessage dialog = new OpenDialogMessage();
|
||||
|
||||
var res=WeakReferenceMessenger.Default.Send<OpenDialogMessage>(dialog);
|
||||
device = await _dialogService.ShowAddDeviceDialog();
|
||||
if (device != null)
|
||||
{
|
||||
if (await _deviceRepository.Add(device))
|
||||
{
|
||||
var msg = $"添加设备成功:{device.Name}";
|
||||
_logger.LogInformation(msg);
|
||||
// 添加菜单项
|
||||
MenuBean deviceMenu = new MenuBean()
|
||||
{
|
||||
Name = device.Name,
|
||||
Icon = SegoeFluentIcons.Devices4.Glyph,
|
||||
};
|
||||
var rows = await _menuRepository.AddDeviceMenu(deviceMenu);
|
||||
if (rows > 0)
|
||||
{
|
||||
WeakReferenceMessenger.Default.Send<UpdateMenuMessage>(new UpdateMenuMessage(2));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var msg = $"添加设备失败:{device.Name}";
|
||||
_logger.LogInformation(msg);
|
||||
}
|
||||
}
|
||||
|
||||
// OpenDialogMessage dialog = new OpenDialogMessage();
|
||||
//
|
||||
// var res=WeakReferenceMessenger.Default.Send<OpenDialogMessage>(dialog);
|
||||
|
||||
Console.WriteLine("");
|
||||
|
||||
// device = await _deviceDialogService.ShowAddDeviceDialog();
|
||||
// if (device != null)
|
||||
// {
|
||||
// var isOk = await _devicesRepositories.Add(device);
|
||||
// var isOk = await _deviceRepository.Add(device);
|
||||
// if (isOk)
|
||||
// {
|
||||
// // 添加菜单项
|
||||
// MenuBean deviceMenu = new MenuBean()
|
||||
// { Name = device.Name, Icon = SegoeFluentIcons.Devices4.Glyph, ParentId = 2 };
|
||||
// MenuRepositories mre = new MenuRepositories();
|
||||
// MenuRepository mre = new MenuRepository();
|
||||
// mre.AddMenu(deviceMenu);
|
||||
//
|
||||
// // MessageBox.Show("Device added successfully");
|
||||
|
||||
@@ -7,22 +7,21 @@ namespace PMSWPF.ViewModels.Dialogs;
|
||||
|
||||
public partial class DeviceDialogViewModel : ObservableObject
|
||||
{
|
||||
private readonly Device _saveDevice;
|
||||
[ObservableProperty]
|
||||
private Device _device;
|
||||
|
||||
[ObservableProperty] private Device device;
|
||||
|
||||
[ObservableProperty] private string title = "添加设备";
|
||||
|
||||
public DeviceDialogViewModel(Device saveDevice)
|
||||
public DeviceDialogViewModel(Device device)
|
||||
{
|
||||
_saveDevice = saveDevice;
|
||||
device = new Device();
|
||||
_device = device;
|
||||
}
|
||||
|
||||
|
||||
[RelayCommand]
|
||||
public void AddDevice()
|
||||
{
|
||||
device.CopyTo(_saveDevice);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using PMSWPF.Data.Entities;
|
||||
using PMSWPF.Data.Repositories;
|
||||
using PMSWPF.Message;
|
||||
using PMSWPF.Models;
|
||||
using PMSWPF.Services;
|
||||
|
||||
@@ -16,20 +18,34 @@ public partial class MainViewModel : ViewModelBase
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<MenuBean> _menus;
|
||||
|
||||
private readonly MenuRepository _menuRepository;
|
||||
|
||||
public MainViewModel(NavgatorServices navgatorServices)
|
||||
{
|
||||
_navgatorServices = navgatorServices;
|
||||
_menuRepository = new MenuRepository();
|
||||
_navgatorServices.OnViewModelChanged += () => { CurrentViewModel = _navgatorServices.CurrentViewModel; };
|
||||
CurrentViewModel = new HomeViewModel();
|
||||
CurrentViewModel.OnLoaded();
|
||||
|
||||
WeakReferenceMessenger.Default.Register<UpdateMenuMessage>( this,UpdateMenu);
|
||||
|
||||
}
|
||||
|
||||
private async void UpdateMenu(object recipient, UpdateMenuMessage message)
|
||||
{
|
||||
await LoadMenu();
|
||||
}
|
||||
|
||||
|
||||
public override async void OnLoaded()
|
||||
{
|
||||
MenuRepositories mr = new MenuRepositories();
|
||||
var menuList= await mr.GetMenu();
|
||||
Menus=new ObservableCollection<MenuBean>(menuList);
|
||||
{
|
||||
await LoadMenu();
|
||||
}
|
||||
|
||||
private async Task LoadMenu()
|
||||
{
|
||||
var menuList= await _menuRepository.GetMenu();
|
||||
Menus=new ObservableCollection<MenuBean>(menuList);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user