diff --git a/App.xaml.cs b/App.xaml.cs index ffc61e8..a0bd539 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -54,6 +54,8 @@ public partial class App : Application container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); + container.AddScoped(); + container.AddScoped(); Services = container.BuildServiceProvider(); // 启动服务 Services.GetRequiredService(); diff --git a/Models/MenuBean.cs b/Models/MenuBean.cs index 1422aef..0edce88 100644 --- a/Models/MenuBean.cs +++ b/Models/MenuBean.cs @@ -1,4 +1,5 @@ using PMSWPF.Enums; +using PMSWPF.ViewModels; namespace PMSWPF.Models; @@ -11,6 +12,8 @@ public class MenuBean public int ParentId { get; set; } public int DataId { get; set; } public MenuType Type { get; set; } + + public ViewModelBase ViewModel { get; set; } public Object? Data { get; set; } public List Items { get; set; } } \ No newline at end of file diff --git a/Services/DataServices.cs b/Services/DataServices.cs index 8e400ca..74a4040 100644 --- a/Services/DataServices.cs +++ b/Services/DataServices.cs @@ -1,5 +1,6 @@ using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Messaging; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using PMSWPF.Data.Repositories; using PMSWPF.Enums; @@ -7,6 +8,7 @@ using PMSWPF.Extensions; using PMSWPF.Helper; using PMSWPF.Message; using PMSWPF.Models; +using PMSWPF.ViewModels; namespace PMSWPF.Services; @@ -61,13 +63,16 @@ public partial class DataServices : ObservableRecipient, IRecipient switch (menuBean.Type) { case MenuType.MainMenu: + menuBean.ViewModel= GetMainViewModel(menuBean.Name); break; case MenuType.DeviceMenu: + menuBean.ViewModel = App.Current.Services.GetRequiredService(); menuBean.Data= Devices.FirstOrDefault(d => d.Id == menuBean.DataId); break; case MenuType.VariableTableMenu: - menuBean.Data= FindVarTableForDevice(menuBean.DataId); - // menuBean.Data= Devices.FirstOrDefault(d => d.Id == menuBean.DataId); + var varTableVM = App.Current.Services.GetRequiredService(); + varTableVM.VariableTable = FindVarTableForDevice(menuBean.DataId); + menuBean.ViewModel = varTableVM; break; case MenuType.AddVariableTableMenu: break; @@ -79,6 +84,28 @@ public partial class DataServices : ObservableRecipient, IRecipient } } + private ViewModelBase GetMainViewModel(string name) + { + ViewModelBase navgateVM = App.Current.Services.GetRequiredService(); + switch (name) + { + case "主页": + navgateVM = App.Current.Services.GetRequiredService(); + break; + case "设备": + navgateVM = App.Current.Services.GetRequiredService(); + break; + case "数据转换": + navgateVM = App.Current.Services.GetRequiredService(); + break; + case "设置": + navgateVM = App.Current.Services.GetRequiredService(); + break; + } + return navgateVM; + + } + private VariableTable FindVarTableForDevice(int vtableId) { VariableTable varTable = null; diff --git a/ViewModels/DeviceDetailViewModel.cs b/ViewModels/DeviceDetailViewModel.cs new file mode 100644 index 0000000..f53a173 --- /dev/null +++ b/ViewModels/DeviceDetailViewModel.cs @@ -0,0 +1,9 @@ +namespace PMSWPF.ViewModels; + +public class DeviceDetailViewModel:ViewModelBase +{ + public override void OnLoaded() + { + + } +} \ No newline at end of file diff --git a/ViewModels/VariableTableViewModel.cs b/ViewModels/VariableTableViewModel.cs index 3d73a63..34bc1d2 100644 --- a/ViewModels/VariableTableViewModel.cs +++ b/ViewModels/VariableTableViewModel.cs @@ -1,7 +1,12 @@ +using CommunityToolkit.Mvvm.ComponentModel; +using PMSWPF.Models; + namespace PMSWPF.ViewModels; -public class VariableTableViewModel : ViewModelBase +partial class VariableTableViewModel : ViewModelBase { + [ObservableProperty] + private VariableTable variableTable; public override void OnLoaded() { } diff --git a/Views/DeviceDetailView.xaml b/Views/DeviceDetailView.xaml new file mode 100644 index 0000000..3958a0b --- /dev/null +++ b/Views/DeviceDetailView.xaml @@ -0,0 +1,12 @@ + + + + + diff --git a/Views/DeviceDetailView.xaml.cs b/Views/DeviceDetailView.xaml.cs new file mode 100644 index 0000000..57babf5 --- /dev/null +++ b/Views/DeviceDetailView.xaml.cs @@ -0,0 +1,14 @@ +using System.Windows.Controls; +using Microsoft.Extensions.DependencyInjection; +using PMSWPF.ViewModels; + +namespace PMSWPF.Views; + +public partial class DeviceDetailView : UserControl +{ + public DeviceDetailView() + { + InitializeComponent(); + DataContext = App.Current.Services.GetRequiredService(); + } +} \ No newline at end of file diff --git a/Views/DevicesView.xaml b/Views/DevicesView.xaml index 2af27a4..40fe506 100644 --- a/Views/DevicesView.xaml +++ b/Views/DevicesView.xaml @@ -76,7 +76,6 @@ ItemsSource="{Binding Devices}" ItemTemplate="{StaticResource DeviceItemTemplate}" SelectionMode="Single" /> - \ No newline at end of file diff --git a/Views/DevicesView.xaml.cs b/Views/DevicesView.xaml.cs index c4e326d..97ba583 100644 --- a/Views/DevicesView.xaml.cs +++ b/Views/DevicesView.xaml.cs @@ -15,9 +15,4 @@ public partial class DevicesView : UserControl { } - private async void DevicesView_OnLoaded(object sender, RoutedEventArgs e) - { - // var devicesViewModel = (DevicesViewModel)this.DataContext; - // await devicesViewModel.OnLoadedAsync(); - } } \ No newline at end of file diff --git a/Views/MainView.xaml b/Views/MainView.xaml index d1b4a76..ae8f9b3 100644 --- a/Views/MainView.xaml +++ b/Views/MainView.xaml @@ -40,45 +40,9 @@ SelectionFollowsFocus="Disabled" MenuItemsSource="{Binding Menus}" MenuItemTemplate="{StaticResource NavigationViewMenuItem}" - ItemInvoked="NavigationView_OnItemInvoked" - SelectionChanged="NavigationView_OnSelectionChanged" + SelectionChanged="NavigationView_SelectionChanged" > - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -89,21 +53,33 @@ - + + + + + + + + + + + + + diff --git a/Views/MainView.xaml.cs b/Views/MainView.xaml.cs index f8a7535..7b6133d 100644 --- a/Views/MainView.xaml.cs +++ b/Views/MainView.xaml.cs @@ -4,6 +4,7 @@ using CommunityToolkit.Mvvm.Messaging; using iNKORE.UI.WPF.Modern.Controls; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using PMSWPF.Enums; using PMSWPF.Helper; using PMSWPF.Message; using PMSWPF.Models; @@ -18,6 +19,7 @@ public partial class MainView : Window { private readonly ILogger _logger; private MainViewModel _viewModel; + public MainView(MainViewModel viewModel, ILogger logger) { InitializeComponent(); @@ -34,33 +36,31 @@ public partial class MainView : Window /// private void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args) { - var item = args.SelectedItem as NavigationViewItem; - ViewModelBase navgateVM = App.Current.Services.GetRequiredService(); - switch (item.Tag) + try { - case "Home": - // mainViewModel.NavgateTo(); - navgateVM = App.Current.Services.GetRequiredService(); - _logger.LogInformation("导航到到主页面"); - break; - case "Devices": - navgateVM = App.Current.Services.GetRequiredService(); - // mainViewModel.NavgateTo(); - _logger.LogInformation("导航到到设备页面"); - break; - case "DataTransform": - navgateVM = App.Current.Services.GetRequiredService(); - // mainViewModel.NavgateTo(); - _logger.LogInformation("导航到到数据转换页面"); - break; - case "Setting": - // mainViewModel.NavgateTo(); - navgateVM = App.Current.Services.GetRequiredService(); - _logger.LogInformation("导航到到设备页面"); - break; + var menu = args.SelectedItem as MenuBean; + if (menu == null) + throw new ArgumentException("选择的菜单项为空!"); + + switch (menu.Type) + { + case MenuType.MainMenu: + case MenuType.DeviceMenu: + case MenuType.VariableTableMenu: + if (menu.ViewModel == null) + throw new ArgumentException($"菜单项:{menu.Name},没有绑定对象的ViewModel"); + MessageHelper.SendNavgatorMessage(menu.ViewModel); + _logger.LogInformation($"导航到:{menu.Name}"); + break; + case MenuType.AddVariableTableMenu: + break; + } + } + catch (Exception e) + { + NotificationHelper.ShowMessage(e.Message, NotificationType.Error); + _logger.LogError(e.Message); } - - MessageHelper.SendNavgatorMessage(navgateVM); } private async void MainView_OnLoaded(object sender, RoutedEventArgs e) @@ -71,7 +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 "主页": @@ -95,13 +95,7 @@ public partial class MainView : Window _logger.LogInformation("导航到到设备页面"); break; } - - MessageHelper.SendNavgatorMessage(navgateVM,parameter); - } - - private void NavigationView_OnSelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args) - { - var selectMenu= args.SelectedItem as MenuBean; + MessageHelper.SendNavgatorMessage(navgateVM); } } \ No newline at end of file diff --git a/Views/VariableTableView.xaml b/Views/VariableTableView.xaml index ccd0141..5af6b50 100644 --- a/Views/VariableTableView.xaml +++ b/Views/VariableTableView.xaml @@ -4,15 +4,17 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf" + xmlns:vm="clr-namespace:PMSWPF.ViewModels" mc:Ignorable="d" + d:DataContext="{d:DesignInstance vm:VariableTableViewModel}" d:DesignHeight="300" d:DesignWidth="300"> - + - + diff --git a/Views/VariableTableView.xaml.cs b/Views/VariableTableView.xaml.cs index eea4ae2..6376421 100644 --- a/Views/VariableTableView.xaml.cs +++ b/Views/VariableTableView.xaml.cs @@ -1,13 +1,14 @@ using System.Windows.Controls; +using Microsoft.Extensions.DependencyInjection; using PMSWPF.ViewModels; namespace PMSWPF.Views; public partial class VariableTableView : UserControl { - public VariableTableView(VariableTableViewModel viewModel) + public VariableTableView() { InitializeComponent(); - DataContext = viewModel; + DataContext = App.Current.Services.GetRequiredService(); } } \ No newline at end of file