diff --git a/App.xaml.cs b/App.xaml.cs index ff072f2..37230d8 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -8,6 +8,7 @@ using NLog.Extensions.Logging; using PMSWPF.Data; using PMSWPF.Data.Entities; using PMSWPF.Data.Repositories; +using PMSWPF.Enums; using PMSWPF.Services; using PMSWPF.ViewModels; using PMSWPF.Views; @@ -34,8 +35,9 @@ public partial class App : Application }); + container.AddSingleton(); container.AddSingleton(); - container.AddSingleton(); + container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); @@ -72,16 +74,18 @@ public partial class App : Application using (var db = DbContext.GetInstance()) { List items = new List(); - 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}); + items.Add(new DbMenu() + { Name = "主页", Type = MenuType.MainMenu, Icon = SegoeFluentIcons.Home.Glyph, ParentId = 0 }); + items.Add(new DbMenu() + { Name = "设备", Type = MenuType.MainMenu, Icon = SegoeFluentIcons.Devices3.Glyph, ParentId = 0 }); + items.Add(new DbMenu() + { Name = "数据转换", Type = MenuType.MainMenu, Icon = SegoeFluentIcons.ChromeSwitch.Glyph, ParentId = 0 }); + items.Add(new DbMenu() + { Name = "设置", Type = MenuType.MainMenu, Icon = SegoeFluentIcons.Settings.Glyph, ParentId = 0 }); + items.Add(new DbMenu() + { Name = "关于", Type = MenuType.MainMenu, Icon = SegoeFluentIcons.Info.Glyph, ParentId = 0 }); db.Insertable(items).ExecuteCommand(); } - - - } private void InitDB() diff --git a/Data/Entities/DbMenu.cs b/Data/Entities/DbMenu.cs index 4f6572b..681ce3a 100644 --- a/Data/Entities/DbMenu.cs +++ b/Data/Entities/DbMenu.cs @@ -1,4 +1,6 @@ +using PMSWPF.Enums; using SqlSugar; +using SqlSugar.DbConvert; namespace PMSWPF.Data.Entities; @@ -10,6 +12,8 @@ public class DbMenu public string Icon { get; set; } public string Name { get; set; } public int ParentId { get; set; } + [SugarColumn(ColumnDataType = "varchar(20)", SqlParameterDbType = typeof(EnumToStringConvert))] + public MenuType Type { get; set; } [SugarColumn(IsIgnore = true)] public List Items { get; set; } diff --git a/Data/Repositories/MenuRepository.cs b/Data/Repositories/MenuRepository.cs index 5c593ac..576be89 100644 --- a/Data/Repositories/MenuRepository.cs +++ b/Data/Repositories/MenuRepository.cs @@ -63,12 +63,14 @@ public class MenuRepository { Name = "默认变量表", Icon = SegoeFluentIcons.Tablet.Glyph, + Type = MenuType.VariableTableMenu, ParentId = addDM.Id, }; var addVarTable=new MenuBean() { Name = "添加变量表", Icon = SegoeFluentIcons.Add.Glyph, + Type = MenuType.AddVariableTableMenu, ParentId = addDM.Id, }; var defTableRes = await _db.Insertable(defVarTable).ExecuteCommandAsync(); diff --git a/Enums/LoadTypes.cs b/Enums/LoadTypes.cs new file mode 100644 index 0000000..e23ccf6 --- /dev/null +++ b/Enums/LoadTypes.cs @@ -0,0 +1,8 @@ +namespace PMSWPF.Enums; + +public enum LoadTypes +{ + Devices, + Menu, + Mqtt +} \ No newline at end of file diff --git a/Enums/MenuType.cs b/Enums/MenuType.cs new file mode 100644 index 0000000..431a472 --- /dev/null +++ b/Enums/MenuType.cs @@ -0,0 +1,9 @@ +namespace PMSWPF.Enums; + +public enum MenuType +{ + MainMenu, + DeviceMenu, + VariableTableMenu, + AddVariableTableMenu, +} \ No newline at end of file diff --git a/Extensions/TaskExtensions.cs b/Extensions/TaskExtensions.cs index c666eb1..261bbba 100644 --- a/Extensions/TaskExtensions.cs +++ b/Extensions/TaskExtensions.cs @@ -14,4 +14,16 @@ public static class TaskExtensions onError?.Invoke(e); } } + public static async Task Await(this Task task, Action onError = null, Action onComplete = null) + { + try + { + T res= await task; + onComplete?.Invoke(res); + } + catch (Exception e) + { + onError?.Invoke(e); + } + } } \ No newline at end of file diff --git a/Message/LoadMessage.cs b/Message/LoadMessage.cs new file mode 100644 index 0000000..c743844 --- /dev/null +++ b/Message/LoadMessage.cs @@ -0,0 +1,11 @@ +using CommunityToolkit.Mvvm.Messaging.Messages; +using PMSWPF.Enums; + +namespace PMSWPF.Message; + +public class LoadMessage:ValueChangedMessage +{ + public LoadMessage(LoadTypes types) : base(types) + { + } +} \ No newline at end of file diff --git a/Message/NavgatorMessage.cs b/Message/NavgatorMessage.cs index 05fd36c..4d1215c 100644 --- a/Message/NavgatorMessage.cs +++ b/Message/NavgatorMessage.cs @@ -5,7 +5,9 @@ namespace PMSWPF.Message; public class NavgatorMessage : ValueChangedMessage { - public NavgatorMessage(ViewModelBase value) : base(value) + public Object Parameters; + public NavgatorMessage(ViewModelBase value,Object parameters=null) : base(value) { + Parameters=parameters; } } \ No newline at end of file diff --git a/Models/MenuBean.cs b/Models/MenuBean.cs index e6a3a7a..0a047c8 100644 --- a/Models/MenuBean.cs +++ b/Models/MenuBean.cs @@ -1,3 +1,5 @@ +using PMSWPF.Enums; + namespace PMSWPF.Models; public class MenuBean @@ -7,5 +9,7 @@ public class MenuBean public string Icon { get; set; } public string Name { get; set; } public int ParentId { get; set; } + + public MenuType Type { get; set; } public List Items { get; set; } } \ No newline at end of file diff --git a/Services/DataServices.cs b/Services/DataServices.cs new file mode 100644 index 0000000..4220d3d --- /dev/null +++ b/Services/DataServices.cs @@ -0,0 +1,76 @@ +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Messaging; +using Microsoft.Extensions.Logging; +using PMSWPF.Data.Repositories; +using PMSWPF.Enums; +using PMSWPF.Extensions; +using PMSWPF.Helper; +using PMSWPF.Message; +using PMSWPF.Models; + +namespace PMSWPF.Services; + +public partial class DataServices:ObservableRecipient,IRecipient +{ + private readonly ILogger _logger; + [ObservableProperty] + private List _devices = new List(); + [ObservableProperty] + private List menuBeans = new List(); + private readonly DeviceRepository _deviceRepository; + private readonly MenuRepository _menuRepository; + + public event Action> OnDeviceListChanged; + public event Action> OnMenuListChanged; + + + partial void OnDevicesChanged(List devices) + { + OnDeviceListChanged?.Invoke(devices); + } + + partial void OnMenuBeansChanged(List menuBeans) + { + OnMenuListChanged?.Invoke(menuBeans); + } + + + public DataServices(ILogger logger ) + { + _logger = logger; + IsActive = true; + _deviceRepository = new DeviceRepository(); + _menuRepository = new MenuRepository(); + + } + + + + public async void Receive(LoadMessage message) + { + + if (!(message.Value is LoadTypes)) + throw new ArgumentException($"接受到的加载类型错误:{message.Value}"); + try + { + + switch ((LoadTypes)message.Value ) + { + case LoadTypes.Devices: + Devices= await _deviceRepository.GetAll(); + break; + case LoadTypes.Menu: + MenuBeans= await _menuRepository.GetMenu(); + break; + + } + } + catch (Exception e) + { + NotificationHelper.ShowMessage($"加载数据出现了错误:{e.Message}"); + _logger.LogError($"加载数据出现了错误:{e.Message}"); + + } + + } +} \ No newline at end of file diff --git a/Services/NavgatorServices.cs b/Services/NavgatorServices.cs index 15975b5..0d91141 100644 --- a/Services/NavgatorServices.cs +++ b/Services/NavgatorServices.cs @@ -29,6 +29,7 @@ public class NavgatorServices : ObservableRecipient, IRecipient public void Receive(NavgatorMessage message) { CurrentViewModel = message.Value; + CurrentViewModel.NavgateParameters = message.Parameters; } public event Action OnViewModelChanged; diff --git a/ViewModels/DevicesViewModel.cs b/ViewModels/DevicesViewModel.cs index c8464bd..9b8ff60 100644 --- a/ViewModels/DevicesViewModel.cs +++ b/ViewModels/DevicesViewModel.cs @@ -19,24 +19,27 @@ public partial class DevicesViewModel : ViewModelBase private readonly DeviceRepository _deviceRepository; private readonly ILogger _logger; private readonly IDialogService _dialogService; + private readonly DataServices _dataServices; [ObservableProperty] private ObservableCollection _devices; private readonly MenuRepository _menuRepository; public DevicesViewModel( - ILogger logger, IDialogService dialogService + ILogger logger, IDialogService dialogService, DataServices dataServices ) { _deviceRepository = new DeviceRepository(); _menuRepository = new MenuRepository(); _logger = logger; _dialogService = dialogService; + _dataServices = dataServices; + + WeakReferenceMessenger.Default.Send(new LoadMessage(LoadTypes.Devices)); + _dataServices.OnDeviceListChanged += (devices) => { Devices = new ObservableCollection(devices); }; } public async Task OnLoadedAsync() { - var ds = await _deviceRepository.GetAll(); - Devices = new ObservableCollection(ds); } [RelayCommand] @@ -56,6 +59,7 @@ public partial class DevicesViewModel : ViewModelBase MenuBean deviceMenu = new MenuBean() { Name = device.Name, + Type = MenuType.DeviceMenu, Icon = SegoeFluentIcons.Devices4.Glyph, }; bool addMenuRes = await _menuRepository.AddDeviceMenu(deviceMenu); diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs index 7e440b4..d797810 100644 --- a/ViewModels/MainViewModel.cs +++ b/ViewModels/MainViewModel.cs @@ -4,6 +4,7 @@ using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using PMSWPF.Data.Entities; using PMSWPF.Data.Repositories; +using PMSWPF.Enums; using PMSWPF.Message; using PMSWPF.Models; using PMSWPF.Services; @@ -13,6 +14,7 @@ namespace PMSWPF.ViewModels; public partial class MainViewModel : ViewModelBase { private readonly NavgatorServices _navgatorServices; + private readonly DataServices _dataServices; [ObservableProperty] private ViewModelBase currentViewModel; [ObservableProperty] @@ -20,32 +22,26 @@ public partial class MainViewModel : ViewModelBase private readonly MenuRepository _menuRepository; - public MainViewModel(NavgatorServices navgatorServices) + public MainViewModel(NavgatorServices navgatorServices,DataServices dataServices) { _navgatorServices = navgatorServices; - _menuRepository = new MenuRepository(); + _dataServices = dataServices; + _navgatorServices.OnViewModelChanged += () => { CurrentViewModel = _navgatorServices.CurrentViewModel; }; CurrentViewModel = new HomeViewModel(); CurrentViewModel.OnLoaded(); - WeakReferenceMessenger.Default.Register( this,UpdateMenu); + WeakReferenceMessenger.Default.Send(new LoadMessage(LoadTypes.Menu)); + dataServices.OnMenuListChanged += (menus) => + { + Menus = new ObservableCollection(menus); + }; + + } + + + public override void OnLoaded() + { } - - private async void UpdateMenu(object recipient, UpdateMenuMessage message) - { - await LoadMenu(); - } - - - public override async void OnLoaded() - { - await LoadMenu(); - } - - private async Task LoadMenu() - { - var menuList= await _menuRepository.GetMenu(); - Menus=new ObservableCollection(menuList); - } } \ No newline at end of file diff --git a/ViewModels/ViewModelBase.cs b/ViewModels/ViewModelBase.cs index 6bf7cbf..c16e622 100644 --- a/ViewModels/ViewModelBase.cs +++ b/ViewModels/ViewModelBase.cs @@ -4,5 +4,6 @@ namespace PMSWPF.ViewModels; public abstract class ViewModelBase : ObservableObject { + public Object NavgateParameters { get; set; } public abstract void OnLoaded(); } \ No newline at end of file diff --git a/Views/MainView.xaml b/Views/MainView.xaml index b7b7543..d1b4a76 100644 --- a/Views/MainView.xaml +++ b/Views/MainView.xaml @@ -41,6 +41,7 @@ MenuItemsSource="{Binding Menus}" MenuItemTemplate="{StaticResource NavigationViewMenuItem}" ItemInvoked="NavigationView_OnItemInvoked" + SelectionChanged="NavigationView_OnSelectionChanged" > diff --git a/Views/MainView.xaml.cs b/Views/MainView.xaml.cs index 34f537c..96375e7 100644 --- a/Views/MainView.xaml.cs +++ b/Views/MainView.xaml.cs @@ -1,9 +1,11 @@ using System.Windows; +using System.Windows.Controls; using CommunityToolkit.Mvvm.Messaging; using iNKORE.UI.WPF.Modern.Controls; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using PMSWPF.Message; +using PMSWPF.Models; using PMSWPF.ViewModels; namespace PMSWPF.Views; @@ -69,6 +71,7 @@ public partial class MainView : Window private void NavigationView_OnItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args) { ViewModelBase navgateVM = App.Current.Services.GetRequiredService(); + Object parameter =null; switch (args.InvokedItem) { case "主页": @@ -93,7 +96,13 @@ public partial class MainView : Window break; } - var nm = new NavgatorMessage(navgateVM); + var nm = new NavgatorMessage(navgateVM,parameter); WeakReferenceMessenger.Default.Send(nm); } + + private void NavigationView_OnSelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args) + { + var selectMenu= args.SelectedItem as MenuBean; + + } } \ No newline at end of file diff --git a/Views/VariableTableView.xaml b/Views/VariableTableView.xaml index 6c1b186..ccd0141 100644 --- a/Views/VariableTableView.xaml +++ b/Views/VariableTableView.xaml @@ -17,6 +17,9 @@ + + +