You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
4.1 KiB
C#
132 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using Avalonia.Controls;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Sln.Wcs.UI.ViewModels.Base;
|
|
using Sln.Wcs.UI.ViewModels.Device;
|
|
using Sln.Wcs.UI.ViewModels.Path;
|
|
using Sln.Wcs.UI.ViewModels.Task;
|
|
using Sln.Wcs.UI.Views;
|
|
|
|
namespace Sln.Wcs.UI.ViewModels;
|
|
|
|
public partial class NavigationViewModel : ObservableObject
|
|
{
|
|
private readonly IServiceProvider _sp;
|
|
private readonly Dictionary<Type, ICrudPageViewModel> _vmCache = new();
|
|
private readonly Dictionary<Type, Control> _viewCache = new();
|
|
private Control? _homeView;
|
|
|
|
public ObservableCollection<TopMenuItem> TopMenuItems { get; } = new();
|
|
|
|
[ObservableProperty]
|
|
private string _currentPageTitle = "首页";
|
|
|
|
public event Action<Control?>? PageChanged;
|
|
|
|
public NavigationViewModel(IServiceProvider sp)
|
|
{
|
|
_sp = sp;
|
|
BuildMenu();
|
|
}
|
|
|
|
public void LoadDefaultPage() => ShowHome();
|
|
|
|
private void BuildMenu()
|
|
{
|
|
TopMenuItems.Clear();
|
|
TopMenuItems.Add(new TopMenuItem("首页", ShowHome));
|
|
TopMenuItems.Add(new TopMenuItem("系统监控", ShowMonitor));
|
|
TopMenuItems.Add(new TopMenuItem("基础数据", new List<SubMenuItem>
|
|
{
|
|
new("库位信息", () => NavigateTo<LocationInfoViewModel>("基础数据")),
|
|
new("物料信息", () => NavigateTo<MaterialInfoViewModel>("基础数据")),
|
|
new("仓库信息", () => NavigateTo<StoreInfoViewModel>("基础数据")),
|
|
}));
|
|
TopMenuItems.Add(new TopMenuItem("设备管理", new List<SubMenuItem>
|
|
{
|
|
new("设备主机", () => NavigateTo<DeviceHostViewModel>("设备管理")),
|
|
new("设备信息", () => NavigateTo<DeviceInfoViewModel>("设备管理")),
|
|
new("设备参数", () => NavigateTo<DeviceParamViewModel>("设备管理")),
|
|
}));
|
|
TopMenuItems.Add(new TopMenuItem("路径管理", new List<SubMenuItem>
|
|
{
|
|
new("路径信息", () => NavigateTo<PathInfoViewModel>("路径管理")),
|
|
new("路径明细", () => NavigateTo<PathDetailsViewModel>("路径管理")),
|
|
}));
|
|
TopMenuItems.Add(new TopMenuItem("任务管理", new List<SubMenuItem>
|
|
{
|
|
new("任务队列", () => NavigateTo<TaskQueueViewModel>("任务管理")),
|
|
new("任务明细", () => NavigateTo<TaskDetailViewModel>("任务管理")),
|
|
}));
|
|
}
|
|
|
|
private string? _currentModule;
|
|
|
|
private void ShowHome()
|
|
{
|
|
if (_homeView == null)
|
|
{
|
|
var vm = _sp.GetRequiredService<HomePageViewModel>();
|
|
_homeView = new HomePageView { DataContext = vm };
|
|
}
|
|
CurrentPageTitle = "首页";
|
|
PageChanged?.Invoke(_homeView);
|
|
}
|
|
|
|
private void ShowMonitor()
|
|
{
|
|
CurrentPageTitle = "系统监控";
|
|
PageChanged?.Invoke(new SystemMonitorView(_sp.GetRequiredService<SystemMonitorViewModel>()));
|
|
}
|
|
|
|
private void NavigateTo<T>(string module) where T : ICrudPageViewModel
|
|
{
|
|
var type = typeof(T);
|
|
if (!_vmCache.TryGetValue(type, out var vm))
|
|
{
|
|
vm = _sp.GetRequiredService<T>();
|
|
_vmCache[type] = vm;
|
|
}
|
|
if (!_viewCache.TryGetValue(type, out var view))
|
|
{
|
|
view = vm.CreateView();
|
|
view.DataContext = vm;
|
|
_viewCache[type] = view;
|
|
}
|
|
CurrentPageTitle = $"{module} > {vm.PageTitle}";
|
|
PageChanged?.Invoke(view);
|
|
vm.Load();
|
|
}
|
|
}
|
|
|
|
public class TopMenuItem
|
|
{
|
|
public string Label { get; set; }
|
|
public List<SubMenuItem>? Children { get; set; }
|
|
public Action? Action { get; set; }
|
|
|
|
public TopMenuItem(string label, Action action)
|
|
{
|
|
Label = label; Action = action;
|
|
}
|
|
|
|
public TopMenuItem(string label, List<SubMenuItem> children)
|
|
{
|
|
Label = label; Children = children;
|
|
}
|
|
}
|
|
|
|
public class SubMenuItem
|
|
{
|
|
public string Label { get; set; }
|
|
public Action Action { get; set; }
|
|
|
|
public SubMenuItem(string label, Action action)
|
|
{
|
|
Label = label; Action = action;
|
|
}
|
|
}
|