完成页面导航功能
This commit is contained in:
@@ -161,6 +161,7 @@ public partial class App : System.Windows.Application
|
||||
services.AddSingleton<IVariableTableAppService,VariableTableAppService>();
|
||||
services.AddSingleton<INavigationService, NavigationService>();
|
||||
services.AddSingleton<IMenuService, MenuService>();
|
||||
services.AddSingleton<IDialogService, DialogService>();
|
||||
//注册WPF中的服务
|
||||
services.AddSingleton<DataServices>();
|
||||
// 注册视图模型
|
||||
@@ -174,7 +175,7 @@ public partial class App : System.Windows.Application
|
||||
services.AddSingleton<VariableTableViewModel>();
|
||||
//services.AddScoped<MqttServerDetailViewModel>();
|
||||
services.AddSingleton<DeviceDetailViewModel>();
|
||||
services.AddScoped<MqttsViewModel>();
|
||||
services.AddSingleton<MqttsViewModel>();
|
||||
//注册View视图
|
||||
services.AddSingleton<SplashWindow>();
|
||||
services.AddSingleton<SettingView>();
|
||||
|
||||
@@ -8,7 +8,7 @@ using iNKORE.UI.WPF.Modern.Controls;
|
||||
|
||||
namespace DMS.WPF.Services;
|
||||
|
||||
public class DialogService
|
||||
public class DialogService :IDialogService
|
||||
{
|
||||
// private readonly DataServices _dataServices;
|
||||
//
|
||||
|
||||
@@ -36,9 +36,8 @@ public class NavigationService : INavigationService
|
||||
return;
|
||||
}
|
||||
|
||||
var mainViewModel = _serviceProvider.GetRequiredService<MainViewModel>();
|
||||
var viewModelType = GetViewModelTypeByKey(viewKey);
|
||||
var viewModel = _serviceProvider.GetRequiredService(viewModelType) as ViewModelBase;
|
||||
var mainViewModel = App.Current.Services.GetRequiredService<MainViewModel>();
|
||||
var viewModel = GetViewModelByKey(viewKey);
|
||||
|
||||
if (viewModel is INavigatable navigatableViewModel)
|
||||
{
|
||||
@@ -49,19 +48,28 @@ public class NavigationService : INavigationService
|
||||
}
|
||||
|
||||
|
||||
|
||||
private Type GetViewModelTypeByKey(string key)
|
||||
private ViewModelBase GetViewModelByKey(string key)
|
||||
{
|
||||
return key switch
|
||||
{
|
||||
"HomeView" => typeof(HomeViewModel),
|
||||
"DevicesView" => typeof(DevicesViewModel),
|
||||
"DeviceDetailView" => typeof(DeviceDetailViewModel),
|
||||
"VariableTableView" => typeof(VariableTableViewModel),
|
||||
"MqttsView" => typeof(MqttsViewModel),
|
||||
"MqttServerDetailView" => typeof(MqttServerDetailViewModel),
|
||||
"SettingView" => typeof(SettingViewModel),
|
||||
_ => throw new KeyNotFoundException($"未找到与键 '{key}' 关联的视图模型类型。请检查 NavigationService 的映射配置。")
|
||||
};
|
||||
switch (key)
|
||||
{
|
||||
case "HomeView":
|
||||
return App.Current.Services.GetRequiredService<HomeViewModel>();
|
||||
case "DevicesView":
|
||||
return App.Current.Services.GetRequiredService<DevicesViewModel>();
|
||||
case "DeviceDetailView":
|
||||
return App.Current.Services.GetRequiredService<DeviceDetailViewModel>();
|
||||
case "DataTransformView":
|
||||
return App.Current.Services.GetRequiredService<DataTransformViewModel>();
|
||||
case "VariableTableView":
|
||||
return App.Current.Services.GetRequiredService<VariableTableViewModel>();
|
||||
case "MqttsView":
|
||||
return App.Current.Services.GetRequiredService<MqttsViewModel>();
|
||||
case "MqttServerDetailView":
|
||||
return App.Current.Services.GetRequiredService<MqttServerDetailViewModel>();
|
||||
case "SettingView":
|
||||
return App.Current.Services.GetRequiredService<SettingViewModel>();
|
||||
default:
|
||||
throw new KeyNotFoundException($"未找到与键 '{key}' 关联的视图模型类型。请检查 NavigationService 的映射配置。");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ namespace DMS.WPF.ViewModels;
|
||||
/// <summary>
|
||||
/// 设备管理视图模型,负责设备的增删改查操作。
|
||||
/// </summary>
|
||||
public partial class DevicesViewModel : ViewModelBase
|
||||
public partial class DevicesViewModel : ViewModelBase,INavigatable
|
||||
{
|
||||
private readonly DataServices _dataServices;
|
||||
private readonly IDialogService _dialogService;
|
||||
@@ -197,4 +197,9 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
deviceDetailVm.CurrentDevice = SelectedDevice;
|
||||
MessageHelper.SendNavgatorMessage(deviceDetailVm);
|
||||
}
|
||||
|
||||
public async Task OnNavigatedToAsync(object parameter)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ using DMS.Helper;
|
||||
using DMS.Services;
|
||||
using DMS.ViewModels;
|
||||
using DMS.WPF.Helper;
|
||||
using DMS.WPF.ViewModels.Items;
|
||||
using iNKORE.UI.WPF.Modern.Common.IconKeys;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -27,13 +28,14 @@ public partial class MainViewModel : ViewModelBase
|
||||
{
|
||||
public DataServices DataServices { get; }
|
||||
private readonly IDialogService _dialogService;
|
||||
private readonly INavigationService _navigationService;
|
||||
private readonly ILogger<MainViewModel> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// 当前显示的视图模型。
|
||||
/// </summary>
|
||||
[ObservableProperty]
|
||||
private ViewModelBase currentViewModel;
|
||||
private ViewModelBase _currentViewModel;
|
||||
|
||||
/// <summary>
|
||||
/// 应用程序的菜单列表。
|
||||
@@ -48,10 +50,11 @@ public partial class MainViewModel : ViewModelBase
|
||||
/// <param name="dataServices">数据服务。</param>
|
||||
/// <param name="dialogService">对话框服务。</param>
|
||||
/// <param name="logger">日志记录器。</param>
|
||||
public MainViewModel(DataServices dataServices,
|
||||
public MainViewModel(DataServices dataServices,INavigationService navigationService,
|
||||
ILogger<MainViewModel> logger)
|
||||
{
|
||||
DataServices = dataServices;
|
||||
_navigationService = navigationService;
|
||||
_logger = logger;
|
||||
|
||||
CurrentViewModel = new HomeViewModel();
|
||||
@@ -161,8 +164,9 @@ public partial class MainViewModel : ViewModelBase
|
||||
/// 处理菜单选择变化的逻辑。
|
||||
/// </summary>
|
||||
/// <param name="menu">被选中的菜单项。</param>
|
||||
public async Task MenuSelectionChanged(MenuBean menu)
|
||||
public async Task MenuSelectionChanged(MenuBeanItemViewModel menu)
|
||||
{
|
||||
_navigationService.NavigateToAsync(menu.TargetViewKey);
|
||||
// try
|
||||
// {
|
||||
// switch (menu.Type)
|
||||
|
||||
@@ -50,13 +50,12 @@ public partial class MqttsViewModel : ViewModelBase
|
||||
private MqttServerItemViewModel _selectedMqtt;
|
||||
|
||||
public MqttsViewModel(
|
||||
ILogger<MqttsViewModel> logger, IDialogService dialogService, DataServices dataServices, NavgatorServices navgatorServices
|
||||
ILogger<MqttsViewModel> logger, IDialogService dialogService, DataServices dataServices
|
||||
)
|
||||
{
|
||||
_logger = logger;
|
||||
_dialogService = dialogService;
|
||||
_dataServices = dataServices;
|
||||
_navgatorServices = navgatorServices;
|
||||
|
||||
// if (dataServices.Mqtts == null || dataServices.Mqtts.Count == 0)
|
||||
// {
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
xmlns:vm="clr-namespace:DMS.WPF.ViewModels"
|
||||
xmlns:mo="clr-namespace:DMS.Core.Models;assembly=DMS.Core"
|
||||
xmlns:hc="https://handyorg.github.io/handycontrol"
|
||||
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
|
||||
xmlns:tb="http://hardcodet.net/taskbar"
|
||||
xmlns:taskbarNotification="http://www.hardcodet.net/taskbar"
|
||||
Title="设备管理系统"
|
||||
@@ -32,7 +33,7 @@
|
||||
<DataTemplate x:Key="NavigationViewMenuItem"
|
||||
DataType="{x:Type mo:MenuBean}">
|
||||
<ui:NavigationViewItem Content="{Binding Header}"
|
||||
|
||||
Tag="{Binding }"
|
||||
MenuItemsSource="{Binding Children }">
|
||||
<ui:NavigationViewItem.Icon>
|
||||
<ui:FontIcon Glyph="{Binding Icon}" />
|
||||
@@ -61,9 +62,10 @@
|
||||
IsBackButtonVisible="Collapsed"
|
||||
IsBackEnabled="False"
|
||||
SelectionFollowsFocus="Disabled"
|
||||
SelectionChanged="NavigationView_SelectionChanged"
|
||||
MenuItemsSource="{Binding DataServices.MenuTrees}"
|
||||
MenuItemTemplate="{StaticResource NavigationViewMenuItem}"
|
||||
SelectionChanged="NavigationView_SelectionChanged">
|
||||
MenuItemTemplate="{StaticResource NavigationViewMenuItem}">
|
||||
|
||||
|
||||
|
||||
<ui:NavigationView.AutoSuggestBox>
|
||||
|
||||
@@ -5,6 +5,7 @@ using DMS.WPF.ViewModels;
|
||||
using iNKORE.UI.WPF.Modern.Controls;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using DMS.Core.Enums;
|
||||
using DMS.WPF.ViewModels.Items;
|
||||
|
||||
namespace DMS.WPF.Views;
|
||||
|
||||
@@ -58,15 +59,12 @@ public partial class MainView : Window
|
||||
/// <param name="args"></param>
|
||||
private async void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
|
||||
{
|
||||
// var menu = args.SelectedItem as MenuBean;
|
||||
// if (menu != null)
|
||||
// {
|
||||
// await _viewModel.MenuSelectionChanged(menu);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// NotificationHelper.ShowError("选择的菜单项为空!");
|
||||
// }
|
||||
var menu = args.SelectedItem as MenuBeanItemViewModel;
|
||||
if (menu != null)
|
||||
{
|
||||
await _viewModel.MenuSelectionChanged(menu);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private async void MainView_OnLoaded(object sender, RoutedEventArgs e)
|
||||
|
||||
Reference in New Issue
Block a user