Files
CapMachine/CapMachine.Wpf/ViewModels/MainViewModel.cs
2026-03-27 12:39:12 +08:00

166 lines
4.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using CapMachine.Core;
using CapMachine.Core.IService;
using CapMachine.Wpf.Models;
using CapMachine.Wpf.PrismEvent;
using CapMachine.Wpf.Services;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using System;
using System.Windows;
using System.Windows.Input;
namespace CapMachine.Wpf.ViewModels
{
public class MainViewModel : NavigationViewModel
{
public MainViewModel(IRegionManager region, INavigationMenuService menuService, SysRunService sysService, IEventAggregator eventAggregator, ConfigService configService)
{
this.region = region;
MenuService = menuService;
SysService = sysService;
EventAggregator = eventAggregator;
ConfigService = configService;
NavigateCommand = new DelegateCommand<NavigationItem>(Navigate);
}
public INavigationMenuService MenuService { get; }
public SysRunService SysService { get; }
public IEventAggregator EventAggregator { get; }
public ConfigService ConfigService { get; }
public DelegateCommand<NavigationItem> NavigateCommand { get; private set; }
private int selectedIndex = -1;
public int SelectedIndex
{
get { return selectedIndex; }
set { selectedIndex = value; RaisePropertyChanged(); }
}
private bool isTopDrawerOpen;
private readonly IRegionManager region;
public bool IsTopDrawerOpen
{
get { return isTopDrawerOpen; }
set
{
isTopDrawerOpen = value;
if (SelectedIndex == 0 && !value)
SelectedIndex = -1;
RaisePropertyChanged();
}
}
private void Navigate(NavigationItem item)
{
if (item == null) return;
if (item.Name.Equals("系统"))
{
IsTopDrawerOpen = true;
return;
}
if (!CanNavigate(item.PageName))
{
IsTopDrawerOpen = false;
return;
}
NavigatePage(item.PageName);
IsTopDrawerOpen = false;
}
/// <summary>
/// 校验当前页面是否允许导航
/// </summary>
/// <param name="pageName">目标页面名称</param>
/// <returns>true允许导航false禁止导航</returns>
private bool CanNavigate(string pageName)
{
if (string.IsNullOrWhiteSpace(pageName))
{
return false;
}
if (pageName.Equals("UserManageView", StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (ConfigService.IsUserLoggedIn)
{
return true;
}
MessageBox.Show("请先登录用户,再切换到其他界面!", "提示");
return false;
}
private DelegateCommand<string> _TopDrawerCmd;
/// <summary>
/// 顶部弹窗按钮命令
/// </summary>
public DelegateCommand<string> TopDrawerCmd
{
set
{
_TopDrawerCmd = value;
}
get
{
if (_TopDrawerCmd == null)
{
_TopDrawerCmd = new DelegateCommand<string>((p) => TopDrawerCmdCall(p));
}
return _TopDrawerCmd;
}
}
/// <summary>
/// 弹窗数据
/// </summary>
/// <param name="p"></param>
private void TopDrawerCmdCall(string Name)
{
//测试数据
IsTopDrawerOpen = false;
EventAggregator.GetEvent<ComDialogEvent>().Publish(new ComDialogMsg() { Name = Name, Par = null });
}
/// <summary>
/// 导航到页面信息
/// </summary>
/// <param name="pageName"></param>
private void NavigatePage(string pageName)
{
region.Regions["MainViewContentRegion"].RequestNavigate(pageName, back =>
{
if (!(bool)back.Result)
{
System.Diagnostics.Debug.WriteLine(back.Error.Message);
}
});
}
public override void OnNavigatedTo(NavigationContext navigationContext)
{
MenuService.Initialize();
//region.Regions["MainViewContentRegion"].RequestNavigate("DashboardView", back =>
//{
// if (!(bool)back.Result)
// {
// }
//});
base.OnNavigatedTo(navigationContext);
}
}
}