修改了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<NavgatorServices>();
|
||||||
container.AddSingleton<DevicesRepositories>();
|
container.AddSingleton<IDialogService,DialogService>();
|
||||||
container.AddSingleton<DialogService>();
|
|
||||||
container.AddSingleton<GrowlNotificationService>();
|
container.AddSingleton<GrowlNotificationService>();
|
||||||
container.AddSingleton<MainViewModel>();
|
container.AddSingleton<MainViewModel>();
|
||||||
container.AddSingleton<HomeViewModel>();
|
container.AddSingleton<HomeViewModel>();
|
||||||
@@ -53,7 +52,6 @@ public partial class App : Application
|
|||||||
Services = container.BuildServiceProvider();
|
Services = container.BuildServiceProvider();
|
||||||
// 启动服务
|
// 启动服务
|
||||||
Services.GetRequiredService<GrowlNotificationService>();
|
Services.GetRequiredService<GrowlNotificationService>();
|
||||||
Services.GetRequiredService<DialogService>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public new static App Current => (App)Application.Current;
|
public new static App Current => (App)Application.Current;
|
||||||
@@ -74,11 +72,11 @@ public partial class App : Application
|
|||||||
using (var db = DbContext.GetInstance())
|
using (var db = DbContext.GetInstance())
|
||||||
{
|
{
|
||||||
List<DbMenu> items = new List<DbMenu>();
|
List<DbMenu> items = new List<DbMenu>();
|
||||||
items.Add(new DbMenu() { Id = 1, Name = "主页", Icon = SegoeFluentIcons.Home.Glyph, ParentId = 0});
|
items.Add(new DbMenu() { Name = "主页", Icon = SegoeFluentIcons.Home.Glyph, ParentId = 0});
|
||||||
items.Add(new DbMenu() { Id = 1, Name = "设备", Icon = SegoeFluentIcons.Devices.Glyph, ParentId = 0});
|
items.Add(new DbMenu() { Name = "设备", Icon = SegoeFluentIcons.Devices.Glyph, ParentId = 0});
|
||||||
items.Add(new DbMenu() { Id = 1, Name = "数据转换", Icon = SegoeFluentIcons.Move.Glyph, ParentId = 0});
|
items.Add(new DbMenu() { Name = "数据转换", Icon = SegoeFluentIcons.Move.Glyph, ParentId = 0});
|
||||||
items.Add(new DbMenu() { Id = 1, Name = "设置", Icon = SegoeFluentIcons.Settings.Glyph, ParentId = 0});
|
items.Add(new DbMenu() { 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.Info.Glyph, ParentId = 0});
|
||||||
db.Insertable<DbMenu>(items).ExecuteCommand();
|
db.Insertable<DbMenu>(items).ExecuteCommand();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,10 +7,10 @@ using SqlSugar;
|
|||||||
|
|
||||||
namespace PMSWPF.Data.Repositories;
|
namespace PMSWPF.Data.Repositories;
|
||||||
|
|
||||||
public class DevicesRepositories
|
public class DeviceRepository
|
||||||
{
|
{
|
||||||
private SqlSugarClient _db;
|
private SqlSugarClient _db;
|
||||||
public DevicesRepositories()
|
public DeviceRepository()
|
||||||
{
|
{
|
||||||
_db = DbContext.GetInstance();
|
_db = DbContext.GetInstance();
|
||||||
}
|
}
|
||||||
@@ -6,30 +6,39 @@ using SqlSugar;
|
|||||||
|
|
||||||
namespace PMSWPF.Data.Repositories;
|
namespace PMSWPF.Data.Repositories;
|
||||||
|
|
||||||
public class MenuRepositories
|
public class MenuRepository
|
||||||
{
|
{
|
||||||
private readonly SqlSugarClient _db;
|
private readonly SqlSugarClient _db;
|
||||||
|
|
||||||
public MenuRepositories()
|
public MenuRepository()
|
||||||
{
|
{
|
||||||
_db=DbContext.GetInstance();
|
_db = DbContext.GetInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<MenuBean>> GetMenu()
|
public async Task<List<MenuBean>> GetMenu()
|
||||||
{
|
{
|
||||||
// //无主键用法新:5.1.4.110
|
// //无主键用法新:5.1.4.110
|
||||||
// db.Queryable<Tree>().ToTree(it=>it.Child,it=>it.ParentId,0,it=>it.Id)//+4重载
|
// db.Queryable<Tree>().ToTree(it=>it.Child,it=>it.ParentId,0,it=>it.Id)//+4重载
|
||||||
List<MenuBean> menus=new();
|
List<MenuBean> menus = new();
|
||||||
var dbMenuList=await _db.Queryable<DbMenu>().ToTreeAsync(dm=>dm.Items,dm=>dm.ParentId,0);
|
var dbMenuList = await _db.Queryable<DbMenu>().ToTreeAsync(dm => dm.Items, dm => dm.ParentId, 0);
|
||||||
foreach (var item in dbMenuList)
|
foreach (var item in dbMenuList)
|
||||||
{
|
{
|
||||||
menus.Add(item.CopyTo<MenuBean>());
|
menus.Add(item.CopyTo<MenuBean>());
|
||||||
}
|
}
|
||||||
|
|
||||||
return menus;
|
return menus;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<int> AddMenu(MenuBean menu)
|
public async Task<int> AddMenu(MenuBean menu)
|
||||||
{
|
{
|
||||||
return await _db.Insertable<DbMenu>(menu.CopyTo<DbMenu>()).ExecuteCommandAsync();
|
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;
|
namespace PMSWPF.Services;
|
||||||
|
|
||||||
public class DialogService:ObservableRecipient,IRecipient<OpenDialogMessage>
|
public class DialogService :IDialogService
|
||||||
{
|
{
|
||||||
public DialogService()
|
public DialogService()
|
||||||
{
|
{
|
||||||
IsActive = true;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Device> ShowAddDeviceDialog()
|
public async Task<Device> ShowAddDeviceDialog()
|
||||||
{
|
{
|
||||||
var device = new Device();
|
var device = new Device();
|
||||||
var ddvm = new DeviceDialogViewModel(device)
|
var dialog = new DeviceDialog(device);
|
||||||
{
|
|
||||||
Title = "添加设备"
|
|
||||||
};
|
|
||||||
|
|
||||||
var dialog = new DeviceDialog(ddvm);
|
|
||||||
var res = await dialog.ShowAsync();
|
var res = await dialog.ShowAsync();
|
||||||
if (res == ContentDialogResult.Primary) return device;
|
if (res == ContentDialogResult.Primary)
|
||||||
|
{
|
||||||
|
return device;
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +36,30 @@ public class DialogService:ObservableRecipient,IRecipient<OpenDialogMessage>
|
|||||||
|
|
||||||
public void Receive(OpenDialogMessage message)
|
public void Receive(OpenDialogMessage message)
|
||||||
{
|
{
|
||||||
|
// DialogMessage response = new DialogMessage();
|
||||||
message.Reply(new DialogMessage(){IsConfirm = true, IsCancel = false});
|
// 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;
|
namespace PMSWPF.Services;
|
||||||
|
|
||||||
public interface IDeviceDialogService
|
public interface IDialogService
|
||||||
{
|
{
|
||||||
Task<Device> ShowAddDeviceDialog();
|
Task<Device> ShowAddDeviceDialog();
|
||||||
|
|
||||||
@@ -16,23 +16,26 @@ namespace PMSWPF.ViewModels;
|
|||||||
|
|
||||||
public partial class DevicesViewModel : ViewModelBase
|
public partial class DevicesViewModel : ViewModelBase
|
||||||
{
|
{
|
||||||
private readonly DevicesRepositories _devicesRepositories;
|
private readonly DeviceRepository _deviceRepository;
|
||||||
private readonly ILogger<DevicesViewModel> _logger;
|
private readonly ILogger<DevicesViewModel> _logger;
|
||||||
|
private readonly IDialogService _dialogService;
|
||||||
|
|
||||||
[ObservableProperty] private ObservableCollection<Device> _devices;
|
[ObservableProperty] private ObservableCollection<Device> _devices;
|
||||||
|
private readonly MenuRepository _menuRepository;
|
||||||
|
|
||||||
public DevicesViewModel(DevicesRepositories devicesRepositories,
|
public DevicesViewModel(
|
||||||
ILogger<DevicesViewModel> logger
|
ILogger<DevicesViewModel> logger, IDialogService dialogService
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
_deviceRepository = new DeviceRepository();
|
||||||
_devicesRepositories = devicesRepositories;
|
_menuRepository = new MenuRepository();
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
_dialogService = dialogService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task OnLoadedAsync()
|
public async Task OnLoadedAsync()
|
||||||
{
|
{
|
||||||
var ds = await _devicesRepositories.GetAll();
|
var ds = await _deviceRepository.GetAll();
|
||||||
Devices = new ObservableCollection<Device>(ds);
|
Devices = new ObservableCollection<Device>(ds);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,22 +45,48 @@ public partial class DevicesViewModel : ViewModelBase
|
|||||||
Device device = null;
|
Device device = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
OpenDialogMessage dialog = new OpenDialogMessage();
|
device = await _dialogService.ShowAddDeviceDialog();
|
||||||
|
if (device != null)
|
||||||
var res=WeakReferenceMessenger.Default.Send<OpenDialogMessage>(dialog);
|
{
|
||||||
|
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("");
|
Console.WriteLine("");
|
||||||
|
|
||||||
// device = await _deviceDialogService.ShowAddDeviceDialog();
|
// device = await _deviceDialogService.ShowAddDeviceDialog();
|
||||||
// if (device != null)
|
// if (device != null)
|
||||||
// {
|
// {
|
||||||
// var isOk = await _devicesRepositories.Add(device);
|
// var isOk = await _deviceRepository.Add(device);
|
||||||
// if (isOk)
|
// if (isOk)
|
||||||
// {
|
// {
|
||||||
// // 添加菜单项
|
// // 添加菜单项
|
||||||
// MenuBean deviceMenu = new MenuBean()
|
// MenuBean deviceMenu = new MenuBean()
|
||||||
// { Name = device.Name, Icon = SegoeFluentIcons.Devices4.Glyph, ParentId = 2 };
|
// { Name = device.Name, Icon = SegoeFluentIcons.Devices4.Glyph, ParentId = 2 };
|
||||||
// MenuRepositories mre = new MenuRepositories();
|
// MenuRepository mre = new MenuRepository();
|
||||||
// mre.AddMenu(deviceMenu);
|
// mre.AddMenu(deviceMenu);
|
||||||
//
|
//
|
||||||
// // MessageBox.Show("Device added successfully");
|
// // MessageBox.Show("Device added successfully");
|
||||||
|
|||||||
@@ -7,22 +7,21 @@ namespace PMSWPF.ViewModels.Dialogs;
|
|||||||
|
|
||||||
public partial class DeviceDialogViewModel : ObservableObject
|
public partial class DeviceDialogViewModel : ObservableObject
|
||||||
{
|
{
|
||||||
private readonly Device _saveDevice;
|
[ObservableProperty]
|
||||||
|
private Device _device;
|
||||||
|
|
||||||
[ObservableProperty] private Device device;
|
|
||||||
|
|
||||||
[ObservableProperty] private string title = "添加设备";
|
[ObservableProperty] private string title = "添加设备";
|
||||||
|
|
||||||
public DeviceDialogViewModel(Device saveDevice)
|
public DeviceDialogViewModel(Device device)
|
||||||
{
|
{
|
||||||
_saveDevice = saveDevice;
|
_device = device;
|
||||||
device = new Device();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[RelayCommand]
|
[RelayCommand]
|
||||||
public void AddDevice()
|
public void AddDevice()
|
||||||
{
|
{
|
||||||
device.CopyTo(_saveDevice);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
using CommunityToolkit.Mvvm.Input;
|
using CommunityToolkit.Mvvm.Input;
|
||||||
|
using CommunityToolkit.Mvvm.Messaging;
|
||||||
using PMSWPF.Data.Entities;
|
using PMSWPF.Data.Entities;
|
||||||
using PMSWPF.Data.Repositories;
|
using PMSWPF.Data.Repositories;
|
||||||
|
using PMSWPF.Message;
|
||||||
using PMSWPF.Models;
|
using PMSWPF.Models;
|
||||||
using PMSWPF.Services;
|
using PMSWPF.Services;
|
||||||
|
|
||||||
@@ -16,20 +18,34 @@ public partial class MainViewModel : ViewModelBase
|
|||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
private ObservableCollection<MenuBean> _menus;
|
private ObservableCollection<MenuBean> _menus;
|
||||||
|
|
||||||
|
private readonly MenuRepository _menuRepository;
|
||||||
|
|
||||||
public MainViewModel(NavgatorServices navgatorServices)
|
public MainViewModel(NavgatorServices navgatorServices)
|
||||||
{
|
{
|
||||||
_navgatorServices = navgatorServices;
|
_navgatorServices = navgatorServices;
|
||||||
|
_menuRepository = new MenuRepository();
|
||||||
_navgatorServices.OnViewModelChanged += () => { CurrentViewModel = _navgatorServices.CurrentViewModel; };
|
_navgatorServices.OnViewModelChanged += () => { CurrentViewModel = _navgatorServices.CurrentViewModel; };
|
||||||
CurrentViewModel = new HomeViewModel();
|
CurrentViewModel = new HomeViewModel();
|
||||||
CurrentViewModel.OnLoaded();
|
CurrentViewModel.OnLoaded();
|
||||||
|
|
||||||
|
WeakReferenceMessenger.Default.Register<UpdateMenuMessage>( this,UpdateMenu);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async void UpdateMenu(object recipient, UpdateMenuMessage message)
|
||||||
|
{
|
||||||
|
await LoadMenu();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public override async void OnLoaded()
|
public override async void OnLoaded()
|
||||||
{
|
{
|
||||||
MenuRepositories mr = new MenuRepositories();
|
await LoadMenu();
|
||||||
var menuList= await mr.GetMenu();
|
}
|
||||||
Menus=new ObservableCollection<MenuBean>(menuList);
|
|
||||||
|
private async Task LoadMenu()
|
||||||
|
{
|
||||||
|
var menuList= await _menuRepository.GetMenu();
|
||||||
|
Menus=new ObservableCollection<MenuBean>(menuList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -13,7 +13,6 @@
|
|||||||
Title="{Binding Title}"
|
Title="{Binding Title}"
|
||||||
CloseButtonText="取消"
|
CloseButtonText="取消"
|
||||||
DefaultButton="Primary"
|
DefaultButton="Primary"
|
||||||
PrimaryButtonCommand="{Binding AddDeviceCommand}"
|
|
||||||
PrimaryButtonText="添加"
|
PrimaryButtonText="添加"
|
||||||
|
|
||||||
d:DataContext="{d:DesignInstance vmd:DeviceDialogViewModel}"
|
d:DataContext="{d:DesignInstance vmd:DeviceDialogViewModel}"
|
||||||
|
|||||||
@@ -1,15 +1,16 @@
|
|||||||
using System.Windows;
|
using System.Windows;
|
||||||
using iNKORE.UI.WPF.Modern.Controls;
|
using iNKORE.UI.WPF.Modern.Controls;
|
||||||
|
using PMSWPF.Models;
|
||||||
using PMSWPF.ViewModels.Dialogs;
|
using PMSWPF.ViewModels.Dialogs;
|
||||||
|
|
||||||
namespace PMSWPF.Views.Dialogs;
|
namespace PMSWPF.Views.Dialogs;
|
||||||
|
|
||||||
public partial class DeviceDialog
|
public partial class DeviceDialog
|
||||||
{
|
{
|
||||||
public DeviceDialog(DeviceDialogViewModel viewModel)
|
public DeviceDialog(Device device)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
DataContext = viewModel;
|
DataContext = new DeviceDialogViewModel(device);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnPrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
|
private void OnPrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
|
||||||
|
|||||||
Reference in New Issue
Block a user