修改了Dialog的调用方式,和实现了添加设备添加到侧边菜单中
This commit is contained in:
14
App.xaml.cs
14
App.xaml.cs
@@ -35,8 +35,7 @@ public partial class App : Application
|
||||
|
||||
|
||||
container.AddSingleton<NavgatorServices>();
|
||||
container.AddSingleton<DevicesRepositories>();
|
||||
container.AddSingleton<DialogService>();
|
||||
container.AddSingleton<IDialogService,DialogService>();
|
||||
container.AddSingleton<GrowlNotificationService>();
|
||||
container.AddSingleton<MainViewModel>();
|
||||
container.AddSingleton<HomeViewModel>();
|
||||
@@ -53,7 +52,6 @@ public partial class App : Application
|
||||
Services = container.BuildServiceProvider();
|
||||
// 启动服务
|
||||
Services.GetRequiredService<GrowlNotificationService>();
|
||||
Services.GetRequiredService<DialogService>();
|
||||
}
|
||||
|
||||
public new static App Current => (App)Application.Current;
|
||||
@@ -74,11 +72,11 @@ public partial class App : Application
|
||||
using (var db = DbContext.GetInstance())
|
||||
{
|
||||
List<DbMenu> items = new List<DbMenu>();
|
||||
items.Add(new DbMenu() { Id = 1, Name = "主页", Icon = SegoeFluentIcons.Home.Glyph, ParentId = 0});
|
||||
items.Add(new DbMenu() { Id = 1, Name = "设备", Icon = SegoeFluentIcons.Devices.Glyph, ParentId = 0});
|
||||
items.Add(new DbMenu() { Id = 1, Name = "数据转换", Icon = SegoeFluentIcons.Move.Glyph, ParentId = 0});
|
||||
items.Add(new DbMenu() { Id = 1, Name = "设置", Icon = SegoeFluentIcons.Settings.Glyph, ParentId = 0});
|
||||
items.Add(new DbMenu() { Id = 1, Name = "关于", Icon = SegoeFluentIcons.Info.Glyph, ParentId = 0});
|
||||
items.Add(new DbMenu() { Name = "主页", Icon = SegoeFluentIcons.Home.Glyph, ParentId = 0});
|
||||
items.Add(new DbMenu() { Name = "设备", Icon = SegoeFluentIcons.Devices.Glyph, ParentId = 0});
|
||||
items.Add(new DbMenu() { Name = "数据转换", Icon = SegoeFluentIcons.Move.Glyph, ParentId = 0});
|
||||
items.Add(new DbMenu() { Name = "设置", Icon = SegoeFluentIcons.Settings.Glyph, ParentId = 0});
|
||||
items.Add(new DbMenu() { Name = "关于", Icon = SegoeFluentIcons.Info.Glyph, ParentId = 0});
|
||||
db.Insertable<DbMenu>(items).ExecuteCommand();
|
||||
}
|
||||
|
||||
|
||||
@@ -7,10 +7,10 @@ using SqlSugar;
|
||||
|
||||
namespace PMSWPF.Data.Repositories;
|
||||
|
||||
public class DevicesRepositories
|
||||
public class DeviceRepository
|
||||
{
|
||||
private SqlSugarClient _db;
|
||||
public DevicesRepositories()
|
||||
public DeviceRepository()
|
||||
{
|
||||
_db = DbContext.GetInstance();
|
||||
}
|
||||
@@ -6,25 +6,26 @@ using SqlSugar;
|
||||
|
||||
namespace PMSWPF.Data.Repositories;
|
||||
|
||||
public class MenuRepositories
|
||||
public class MenuRepository
|
||||
{
|
||||
private readonly SqlSugarClient _db;
|
||||
|
||||
public MenuRepositories()
|
||||
public MenuRepository()
|
||||
{
|
||||
_db=DbContext.GetInstance();
|
||||
_db = DbContext.GetInstance();
|
||||
}
|
||||
|
||||
public async Task<List<MenuBean>> GetMenu()
|
||||
{
|
||||
// //无主键用法新:5.1.4.110
|
||||
// db.Queryable<Tree>().ToTree(it=>it.Child,it=>it.ParentId,0,it=>it.Id)//+4重载
|
||||
List<MenuBean> menus=new();
|
||||
var dbMenuList=await _db.Queryable<DbMenu>().ToTreeAsync(dm=>dm.Items,dm=>dm.ParentId,0);
|
||||
List<MenuBean> menus = new();
|
||||
var dbMenuList = await _db.Queryable<DbMenu>().ToTreeAsync(dm => dm.Items, dm => dm.ParentId, 0);
|
||||
foreach (var item in dbMenuList)
|
||||
{
|
||||
menus.Add(item.CopyTo<MenuBean>());
|
||||
}
|
||||
|
||||
return menus;
|
||||
}
|
||||
|
||||
@@ -32,4 +33,12 @@ public class MenuRepositories
|
||||
{
|
||||
return await _db.Insertable<DbMenu>(menu.CopyTo<DbMenu>()).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
public async Task<int> AddDeviceMenu(MenuBean menu)
|
||||
{
|
||||
var deviceMenu = await _db.Queryable<DbMenu>().FirstAsync(m => m.Name == "设备");
|
||||
if (deviceMenu == null) return 0;
|
||||
menu.ParentId = deviceMenu.Id;
|
||||
return await _db.Insertable<DbMenu>(menu.CopyTo<DbMenu>()).ExecuteCommandAsync();
|
||||
}
|
||||
}
|
||||
10
Message/UpdateMenuMessage.cs
Normal file
10
Message/UpdateMenuMessage.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using CommunityToolkit.Mvvm.Messaging.Messages;
|
||||
|
||||
namespace PMSWPF.Message;
|
||||
|
||||
public class UpdateMenuMessage:ValueChangedMessage<int>
|
||||
{
|
||||
public UpdateMenuMessage(int value) : base(value)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -9,26 +9,22 @@ using PMSWPF.Views.Dialogs;
|
||||
|
||||
namespace PMSWPF.Services;
|
||||
|
||||
public class DialogService:ObservableRecipient,IRecipient<OpenDialogMessage>
|
||||
public class DialogService :IDialogService
|
||||
{
|
||||
public DialogService()
|
||||
{
|
||||
IsActive = true;
|
||||
|
||||
}
|
||||
|
||||
public async Task<Device> ShowAddDeviceDialog()
|
||||
{
|
||||
var device = new Device();
|
||||
var ddvm = new DeviceDialogViewModel(device)
|
||||
{
|
||||
Title = "添加设备"
|
||||
};
|
||||
|
||||
var dialog = new DeviceDialog(ddvm);
|
||||
var dialog = new DeviceDialog(device);
|
||||
var res = await dialog.ShowAsync();
|
||||
if (res == ContentDialogResult.Primary) return device;
|
||||
|
||||
if (res == ContentDialogResult.Primary)
|
||||
{
|
||||
return device;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -40,7 +36,30 @@ public class DialogService:ObservableRecipient,IRecipient<OpenDialogMessage>
|
||||
|
||||
public void Receive(OpenDialogMessage message)
|
||||
{
|
||||
|
||||
message.Reply(new DialogMessage(){IsConfirm = true, IsCancel = false});
|
||||
// DialogMessage response = new DialogMessage();
|
||||
// Device device = new Device();
|
||||
// if (message.Message! != null && message.Message.Request != null && message.Message.Request is Device)
|
||||
// {
|
||||
// device = message.Message.Request as Device;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// var ddvm = new DeviceDialogViewModel(device)
|
||||
// {
|
||||
// Title = "添加设备"
|
||||
// };
|
||||
// var dialog = new DeviceDialog(ddvm);
|
||||
// var res = dialog.ShowAsync().GetAwaiter().GetResult();
|
||||
// if (res == ContentDialogResult.Primary)
|
||||
// {
|
||||
// response.IsConfirm = true;
|
||||
// response.Response = device;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// response.IsCancel = true;
|
||||
// }
|
||||
// }
|
||||
// message.Reply(response);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace PMSWPF.Services;
|
||||
|
||||
public interface IDeviceDialogService
|
||||
public interface IDialogService
|
||||
{
|
||||
Task<Device> ShowAddDeviceDialog();
|
||||
|
||||
@@ -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();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
var res=WeakReferenceMessenger.Default.Send<OpenDialogMessage>(dialog);
|
||||
// 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();
|
||||
await LoadMenu();
|
||||
}
|
||||
|
||||
private async Task LoadMenu()
|
||||
{
|
||||
var menuList= await _menuRepository.GetMenu();
|
||||
Menus=new ObservableCollection<MenuBean>(menuList);
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,6 @@
|
||||
Title="{Binding Title}"
|
||||
CloseButtonText="取消"
|
||||
DefaultButton="Primary"
|
||||
PrimaryButtonCommand="{Binding AddDeviceCommand}"
|
||||
PrimaryButtonText="添加"
|
||||
|
||||
d:DataContext="{d:DesignInstance vmd:DeviceDialogViewModel}"
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
using System.Windows;
|
||||
using iNKORE.UI.WPF.Modern.Controls;
|
||||
using PMSWPF.Models;
|
||||
using PMSWPF.ViewModels.Dialogs;
|
||||
|
||||
namespace PMSWPF.Views.Dialogs;
|
||||
|
||||
public partial class DeviceDialog
|
||||
{
|
||||
public DeviceDialog(DeviceDialogViewModel viewModel)
|
||||
public DeviceDialog(Device device)
|
||||
{
|
||||
InitializeComponent();
|
||||
DataContext = viewModel;
|
||||
DataContext = new DeviceDialogViewModel(device);
|
||||
}
|
||||
|
||||
private void OnPrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
|
||||
|
||||
Reference in New Issue
Block a user