diff --git a/App.xaml.cs b/App.xaml.cs index fd6c215..24514b4 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -36,7 +36,7 @@ public partial class App : Application container.AddSingleton(); container.AddSingleton(); - container.AddSingleton(); + container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); @@ -53,6 +53,7 @@ public partial class App : Application Services = container.BuildServiceProvider(); // 启动服务 Services.GetRequiredService(); + Services.GetRequiredService(); } public new static App Current => (App)Application.Current; diff --git a/Data/DbContext.cs b/Data/DbContext.cs index c6cdf44..bdf66e2 100644 --- a/Data/DbContext.cs +++ b/Data/DbContext.cs @@ -4,21 +4,17 @@ namespace PMSWPF.Data; public class DbContext { - private static SqlSugarClient _db; - public static SqlSugarClient GetInstance() { - if (_db == null) + var connectionString = "server=127.0.0.1;port=3306;user=root;password=Pgw15221236646; database=pmswpf;"; + var _db = new SqlSugarClient(new ConnectionConfig { - var connectionString = "server=127.0.0.1;port=3306;user=root;password=Pgw15221236646; database=pmswpf;"; - _db = new SqlSugarClient(new ConnectionConfig - { - ConnectionString = connectionString, - DbType = DbType.MySql, // 根据实际数据库类型修改,如DbType.MySql等 - IsAutoCloseConnection = true, - InitKeyType = InitKeyType.Attribute - }); - } + ConnectionString = connectionString, + DbType = DbType.MySql, // 根据实际数据库类型修改,如DbType.MySql等 + IsAutoCloseConnection = true, + InitKeyType = InitKeyType.Attribute + }); + return _db; } diff --git a/Data/Repositories/MenuRepositories.cs b/Data/Repositories/MenuRepositories.cs index 4b7c8ba..9c81469 100644 --- a/Data/Repositories/MenuRepositories.cs +++ b/Data/Repositories/MenuRepositories.cs @@ -1,5 +1,7 @@ using System.Windows.Controls; using PMSWPF.Data.Entities; +using PMSWPF.Extensions; +using PMSWPF.Models; using SqlSugar; namespace PMSWPF.Data.Repositories; @@ -13,13 +15,21 @@ public class MenuRepositories _db=DbContext.GetInstance(); } - public async Task> GetMenu() + public async Task> GetMenu() { - return await _db.Queryable().ToListAsync(); + // //无主键用法新: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); + foreach (var item in dbMenuList) + { + menus.Add(item.CopyTo()); + } + return menus; } - public async Task AddMenu(DbMenu dbMenu) + public async Task AddMenu(MenuBean menu) { - return await _db.Insertable(dbMenu).ExecuteCommandAsync(); + return await _db.Insertable(menu.CopyTo()).ExecuteCommandAsync(); } } \ No newline at end of file diff --git a/Message/DialogMessage.cs b/Message/DialogMessage.cs new file mode 100644 index 0000000..28c509e --- /dev/null +++ b/Message/DialogMessage.cs @@ -0,0 +1,9 @@ +namespace PMSWPF.Message; + +public class DialogMessage +{ + public bool IsCancel { get; set; } + public bool IsConfirm { get; set; } + public Object? Request { get; set; } + public Object? Response { get; set; } +} \ No newline at end of file diff --git a/Message/OpenDialogMessage.cs b/Message/OpenDialogMessage.cs new file mode 100644 index 0000000..bc16d2a --- /dev/null +++ b/Message/OpenDialogMessage.cs @@ -0,0 +1,21 @@ +using CommunityToolkit.Mvvm.Messaging.Messages; + +namespace PMSWPF.Message; + +public class OpenDialogMessage:RequestMessage +{ + // public bool IsCancel { get; set; } + // public bool IsConfirm { get; set; } + // public Object? Request { get; set; } + // public Object? Response { get; set; } + public DialogMessage Message { get; set; } + + public OpenDialogMessage() + { + } + + public OpenDialogMessage(DialogMessage message) + { + Message = message; + } +} \ No newline at end of file diff --git a/Models/MenuBean.cs b/Models/MenuBean.cs new file mode 100644 index 0000000..e6a3a7a --- /dev/null +++ b/Models/MenuBean.cs @@ -0,0 +1,11 @@ +namespace PMSWPF.Models; + +public class MenuBean +{ + + public int Id { get; set; } + public string Icon { get; set; } + public string Name { get; set; } + public int ParentId { get; set; } + public List Items { get; set; } +} \ No newline at end of file diff --git a/Services/DeviceDialogService.cs b/Services/DialogService.cs similarity index 55% rename from Services/DeviceDialogService.cs rename to Services/DialogService.cs index 24ea1b2..384c326 100644 --- a/Services/DeviceDialogService.cs +++ b/Services/DialogService.cs @@ -1,12 +1,22 @@ -using iNKORE.UI.WPF.Modern.Controls; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Messaging; +using HandyControl.Tools.Extension; +using iNKORE.UI.WPF.Modern.Controls; +using PMSWPF.Message; using PMSWPF.Models; using PMSWPF.ViewModels.Dialogs; using PMSWPF.Views.Dialogs; namespace PMSWPF.Services; -public class DeviceDialogService : IDeviceDialogService +public class DialogService:ObservableRecipient,IRecipient { + public DialogService() + { + IsActive = true; + + } + public async Task ShowAddDeviceDialog() { var device = new Device(); @@ -26,4 +36,11 @@ public class DeviceDialogService : IDeviceDialogService { MessageBox.Show(message); } + + + public void Receive(OpenDialogMessage message) + { + + message.Reply(new DialogMessage(){IsConfirm = true, IsCancel = false}); + } } \ No newline at end of file diff --git a/ViewModels/DevicesViewModel.cs b/ViewModels/DevicesViewModel.cs index 28782df..6932819 100644 --- a/ViewModels/DevicesViewModel.cs +++ b/ViewModels/DevicesViewModel.cs @@ -1,11 +1,14 @@ using System.Collections.ObjectModel; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; +using CommunityToolkit.Mvvm.Messaging; +using iNKORE.UI.WPF.Modern.Common.IconKeys; using Microsoft.Extensions.Logging; using PMSWPF.Data.Repositories; using PMSWPF.Enums; using PMSWPF.Excptions; using PMSWPF.Helper; +using PMSWPF.Message; using PMSWPF.Models; using PMSWPF.Services; @@ -13,17 +16,16 @@ namespace PMSWPF.ViewModels; public partial class DevicesViewModel : ViewModelBase { - private readonly IDeviceDialogService _deviceDialogService; private readonly DevicesRepositories _devicesRepositories; private readonly ILogger _logger; [ObservableProperty] private ObservableCollection _devices; - public DevicesViewModel(IDeviceDialogService deviceDialogService, DevicesRepositories devicesRepositories, + public DevicesViewModel(DevicesRepositories devicesRepositories, ILogger logger ) { - _deviceDialogService = deviceDialogService; + _devicesRepositories = devicesRepositories; _logger = logger; } @@ -40,19 +42,31 @@ public partial class DevicesViewModel : ViewModelBase Device device = null; try { - device = await _deviceDialogService.ShowAddDeviceDialog(); - if (device != null) - { - var isOk = await _devicesRepositories.Add(device); - if (isOk) - { - // MessageBox.Show("Device added successfully"); - await OnLoadedAsync(); - var msg = $"设备添加成功:{device.Name}"; - _logger.LogInformation(msg); - NotificationHelper.ShowMessage(msg, NotificationType.Success); - } - } + 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); + // if (isOk) + // { + // // 添加菜单项 + // MenuBean deviceMenu = new MenuBean() + // { Name = device.Name, Icon = SegoeFluentIcons.Devices4.Glyph, ParentId = 2 }; + // MenuRepositories mre = new MenuRepositories(); + // mre.AddMenu(deviceMenu); + // + // // MessageBox.Show("Device added successfully"); + // await OnLoadedAsync(); + // var msg = $"设备添加成功:{device.Name}"; + // _logger.LogInformation(msg); + // NotificationHelper.ShowMessage(msg, NotificationType.Success); + // } + // } } catch (DbExistException e) { diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs index 15d8b30..5c1cde9 100644 --- a/ViewModels/MainViewModel.cs +++ b/ViewModels/MainViewModel.cs @@ -1,7 +1,9 @@ using System.Collections.ObjectModel; using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; using PMSWPF.Data.Entities; using PMSWPF.Data.Repositories; +using PMSWPF.Models; using PMSWPF.Services; namespace PMSWPF.ViewModels; @@ -12,7 +14,7 @@ public partial class MainViewModel : ViewModelBase [ObservableProperty] private ViewModelBase currentViewModel; [ObservableProperty] - private ObservableCollection _menus; + private ObservableCollection _menus; public MainViewModel(NavgatorServices navgatorServices) { @@ -23,10 +25,11 @@ public partial class MainViewModel : ViewModelBase } + public override async void OnLoaded() - { + { MenuRepositories mr = new MenuRepositories(); var menuList= await mr.GetMenu(); - Menus=new ObservableCollection(menuList); + Menus=new ObservableCollection(menuList); } } \ No newline at end of file diff --git a/Views/MainView.xaml b/Views/MainView.xaml index c86090f..b7b7543 100644 --- a/Views/MainView.xaml +++ b/Views/MainView.xaml @@ -7,15 +7,25 @@ xmlns:local="clr-namespace:PMSWPF.Views" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:vm="clr-namespace:PMSWPF.ViewModels" + xmlns:mo="clr-namespace:PMSWPF.Models" xmlns:hc="https://handyorg.github.io/handycontrol" Title="设备管理系统" Width="1080" Height="800" - + Loaded="MainView_OnLoaded" ui:WindowHelper.UseModernWindowStyle="True" ui:WindowHelper.SystemBackdropType="Mica" d:DataContext="{d:DesignInstance vm:MainViewModel}" mc:Ignorable="d"> + + + + + + + + + + MenuItemsSource="{Binding Menus}" + MenuItemTemplate="{StaticResource NavigationViewMenuItem}" + ItemInvoked="NavigationView_OnItemInvoked" + > - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Views/MainView.xaml.cs b/Views/MainView.xaml.cs index ef0b1aa..34f537c 100644 --- a/Views/MainView.xaml.cs +++ b/Views/MainView.xaml.cs @@ -14,11 +14,12 @@ namespace PMSWPF.Views; public partial class MainView : Window { private readonly ILogger _logger; - + private MainViewModel _viewModel; public MainView(MainViewModel viewModel, ILogger logger) { - _logger = logger; InitializeComponent(); + _viewModel = viewModel; + _logger = logger; DataContext = viewModel; _logger.LogInformation("主界面加载成功"); } @@ -59,4 +60,40 @@ public partial class MainView : Window var nm = new NavgatorMessage(navgateVM); WeakReferenceMessenger.Default.Send(nm); } + + private async void MainView_OnLoaded(object sender, RoutedEventArgs e) + { + _viewModel.OnLoaded(); + } + + private void NavigationView_OnItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args) + { + ViewModelBase navgateVM = App.Current.Services.GetRequiredService(); + switch (args.InvokedItem) + { + case "主页": + // mainViewModel.NavgateTo(); + navgateVM = App.Current.Services.GetRequiredService(); + _logger.LogInformation("导航到到主页面"); + break; + case "设备": + navgateVM = App.Current.Services.GetRequiredService(); + // mainViewModel.NavgateTo(); + _logger.LogInformation("导航到到设备页面"); + break; + case "数据转换": + navgateVM = App.Current.Services.GetRequiredService(); + // mainViewModel.NavgateTo(); + _logger.LogInformation("导航到到数据转换页面"); + break; + case "设置": + // mainViewModel.NavgateTo(); + navgateVM = App.Current.Services.GetRequiredService(); + _logger.LogInformation("导航到到设备页面"); + break; + } + + var nm = new NavgatorMessage(navgateVM); + WeakReferenceMessenger.Default.Send(nm); + } } \ No newline at end of file