diff --git a/App.xaml.cs b/App.xaml.cs index 24514b4..ff072f2 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -35,8 +35,7 @@ public partial class App : Application container.AddSingleton(); - container.AddSingleton(); - container.AddSingleton(); + container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); @@ -53,7 +52,6 @@ public partial class App : Application Services = container.BuildServiceProvider(); // 启动服务 Services.GetRequiredService(); - Services.GetRequiredService(); } public new static App Current => (App)Application.Current; @@ -74,11 +72,11 @@ public partial class App : Application using (var db = DbContext.GetInstance()) { List items = new List(); - 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(items).ExecuteCommand(); } diff --git a/Data/Repositories/DevicesRepositories.cs b/Data/Repositories/DeviceRepository.cs similarity index 96% rename from Data/Repositories/DevicesRepositories.cs rename to Data/Repositories/DeviceRepository.cs index 016118f..25e1c97 100644 --- a/Data/Repositories/DevicesRepositories.cs +++ b/Data/Repositories/DeviceRepository.cs @@ -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(); } diff --git a/Data/Repositories/MenuRepositories.cs b/Data/Repositories/MenuRepository.cs similarity index 54% rename from Data/Repositories/MenuRepositories.cs rename to Data/Repositories/MenuRepository.cs index 9c81469..e4072bd 100644 --- a/Data/Repositories/MenuRepositories.cs +++ b/Data/Repositories/MenuRepository.cs @@ -6,30 +6,39 @@ 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> GetMenu() { // //无主键用法新:5.1.4.110 // db.Queryable().ToTree(it=>it.Child,it=>it.ParentId,0,it=>it.Id)//+4重载 - List menus=new(); - var dbMenuList=await _db.Queryable().ToTreeAsync(dm=>dm.Items,dm=>dm.ParentId,0); + List menus = new(); + var dbMenuList = await _db.Queryable().ToTreeAsync(dm => dm.Items, dm => dm.ParentId, 0); foreach (var item in dbMenuList) { menus.Add(item.CopyTo()); } + return menus; } - + public async Task AddMenu(MenuBean menu) { return await _db.Insertable(menu.CopyTo()).ExecuteCommandAsync(); } + + public async Task AddDeviceMenu(MenuBean menu) + { + var deviceMenu = await _db.Queryable().FirstAsync(m => m.Name == "设备"); + if (deviceMenu == null) return 0; + menu.ParentId = deviceMenu.Id; + return await _db.Insertable(menu.CopyTo()).ExecuteCommandAsync(); + } } \ No newline at end of file diff --git a/Message/UpdateMenuMessage.cs b/Message/UpdateMenuMessage.cs new file mode 100644 index 0000000..34c35cf --- /dev/null +++ b/Message/UpdateMenuMessage.cs @@ -0,0 +1,10 @@ +using CommunityToolkit.Mvvm.Messaging.Messages; + +namespace PMSWPF.Message; + +public class UpdateMenuMessage:ValueChangedMessage +{ + public UpdateMenuMessage(int value) : base(value) + { + } +} \ No newline at end of file diff --git a/Services/DialogService.cs b/Services/DialogService.cs index 384c326..aceb66c 100644 --- a/Services/DialogService.cs +++ b/Services/DialogService.cs @@ -9,26 +9,22 @@ using PMSWPF.Views.Dialogs; namespace PMSWPF.Services; -public class DialogService:ObservableRecipient,IRecipient +public class DialogService :IDialogService { public DialogService() { - IsActive = true; - + } public async Task 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 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); } } \ No newline at end of file diff --git a/Services/IDeviceDialogService.cs b/Services/IDialogService.cs similarity index 80% rename from Services/IDeviceDialogService.cs rename to Services/IDialogService.cs index 2b58c35..db8cec0 100644 --- a/Services/IDeviceDialogService.cs +++ b/Services/IDialogService.cs @@ -2,7 +2,7 @@ namespace PMSWPF.Services; -public interface IDeviceDialogService +public interface IDialogService { Task ShowAddDeviceDialog(); diff --git a/ViewModels/DevicesViewModel.cs b/ViewModels/DevicesViewModel.cs index 6932819..f780de9 100644 --- a/ViewModels/DevicesViewModel.cs +++ b/ViewModels/DevicesViewModel.cs @@ -16,23 +16,26 @@ namespace PMSWPF.ViewModels; public partial class DevicesViewModel : ViewModelBase { - private readonly DevicesRepositories _devicesRepositories; + private readonly DeviceRepository _deviceRepository; private readonly ILogger _logger; + private readonly IDialogService _dialogService; [ObservableProperty] private ObservableCollection _devices; + private readonly MenuRepository _menuRepository; - public DevicesViewModel(DevicesRepositories devicesRepositories, - ILogger logger + public DevicesViewModel( + ILogger 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(ds); } @@ -42,22 +45,48 @@ public partial class DevicesViewModel : ViewModelBase Device device = null; try { - OpenDialogMessage dialog = new OpenDialogMessage(); - - var res=WeakReferenceMessenger.Default.Send(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(new UpdateMenuMessage(2)); + } + } + else + { + var msg = $"添加设备失败:{device.Name}"; + _logger.LogInformation(msg); + } + } + + // OpenDialogMessage dialog = new OpenDialogMessage(); + // + // var res=WeakReferenceMessenger.Default.Send(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"); diff --git a/ViewModels/Dialogs/DeviceDialogViewModel.cs b/ViewModels/Dialogs/DeviceDialogViewModel.cs index 418fede..42fd308 100644 --- a/ViewModels/Dialogs/DeviceDialogViewModel.cs +++ b/ViewModels/Dialogs/DeviceDialogViewModel.cs @@ -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); + } } \ No newline at end of file diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs index 5c1cde9..7e440b4 100644 --- a/ViewModels/MainViewModel.cs +++ b/ViewModels/MainViewModel.cs @@ -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 _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( 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(menuList); + { + await LoadMenu(); + } + + private async Task LoadMenu() + { + var menuList= await _menuRepository.GetMenu(); + Menus=new ObservableCollection(menuList); } } \ No newline at end of file diff --git a/Views/Dialogs/DeviceDialog.xaml b/Views/Dialogs/DeviceDialog.xaml index 170c022..47a2cd5 100644 --- a/Views/Dialogs/DeviceDialog.xaml +++ b/Views/Dialogs/DeviceDialog.xaml @@ -13,7 +13,6 @@ Title="{Binding Title}" CloseButtonText="取消" DefaultButton="Primary" - PrimaryButtonCommand="{Binding AddDeviceCommand}" PrimaryButtonText="添加" d:DataContext="{d:DesignInstance vmd:DeviceDialogViewModel}" diff --git a/Views/Dialogs/DeviceDialog.xaml.cs b/Views/Dialogs/DeviceDialog.xaml.cs index c1cc7a3..ab5d178 100644 --- a/Views/Dialogs/DeviceDialog.xaml.cs +++ b/Views/Dialogs/DeviceDialog.xaml.cs @@ -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)