110 lines
3.8 KiB
C#
110 lines
3.8 KiB
C#
using Prism.Navigation.Regions;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows;
|
||
using System.Windows.Media;
|
||
using UIShare.ViewModelBase;
|
||
|
||
namespace MainModule.ViewModels
|
||
{
|
||
public class ProtocolStartViewModel : NavigateViewModelBase
|
||
{
|
||
#region 私有字段
|
||
private string _testStatus;
|
||
private string _moduleColor;
|
||
#endregion
|
||
|
||
#region 属性
|
||
public string TestStatus
|
||
{
|
||
get => _testStatus;
|
||
set => SetProperty(ref _testStatus, value);
|
||
}
|
||
|
||
public string ModuleColor
|
||
{
|
||
get => _moduleColor;
|
||
set => SetProperty(ref _moduleColor, value);
|
||
}
|
||
#endregion
|
||
|
||
#region 命令
|
||
public DelegateCommand StartProtocolCommand { get; }
|
||
#endregion
|
||
|
||
public ProtocolStartViewModel(IContainerProvider containerProvider) : base(containerProvider)
|
||
{
|
||
StartProtocolCommand = new DelegateCommand(OnStart);
|
||
}
|
||
|
||
#region 命令处理与事件
|
||
private void OnStart()
|
||
{
|
||
// 只在当前格子所属的 Cell Region 内完成切换,不会影响其他格子位置
|
||
SwitchNavigate("AutomatedTestingView");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 定位当前视图所在的 Cell Region(TestCell1..TestCell9),
|
||
/// 在同一个 Region 内完成跳转。Region 位置由 XAML Grid 锁定,永远不会错位。
|
||
/// </summary>
|
||
public void SwitchNavigate(string viewName)
|
||
{
|
||
// 1. 反向查找:哪个 Cell Region 当前托着“我”这个 ProtocolStartView
|
||
for (int i = 1; i <= 9; i++)
|
||
{
|
||
var regionName = $"TestCell{i}";
|
||
if (!_regionManager.Regions.ContainsRegionWithName(regionName)) continue;
|
||
|
||
var region = _regionManager.Regions[regionName];
|
||
var myView = region.Views
|
||
.OfType<FrameworkElement>()
|
||
.FirstOrDefault(v => v.DataContext == this);
|
||
|
||
if (myView == null) continue;
|
||
|
||
// 2. 透传名称与颜色参数,使 AutomatedTestingViewModel 能正确初始化
|
||
var parameters = new NavigationParameters();
|
||
parameters.Add("Name", TestStatus);
|
||
parameters.Add("Color", ModuleColor);
|
||
|
||
// 3. 在本格子 Region 内请求导航,导航成功后再移除旧的 ProtocolStartView 以释放资源
|
||
_regionManager.RequestNavigate(regionName, viewName, navResult =>
|
||
{
|
||
if (navResult.Success == true)
|
||
{
|
||
region.Remove(myView);
|
||
}
|
||
}, parameters);
|
||
_moduleManager.LoadModule("MonitorModule");
|
||
_moduleManager.LoadModule("SettingModule");
|
||
return;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 重写
|
||
public override bool IsNavigationTarget(NavigationContext navigationContext)
|
||
{
|
||
if (navigationContext.Parameters.ContainsKey("Name"))
|
||
return TestStatus == navigationContext.Parameters.GetValue<string>("Name");
|
||
return true;
|
||
}
|
||
|
||
public override void OnNavigatedTo(NavigationContext navigationContext)
|
||
{
|
||
base.OnNavigatedTo(navigationContext);
|
||
|
||
// 接收导航传参:名称与颜色
|
||
if (navigationContext.Parameters.ContainsKey("Name"))
|
||
TestStatus = navigationContext.Parameters.GetValue<string>("Name");
|
||
|
||
if (navigationContext.Parameters.ContainsKey("Color"))
|
||
ModuleColor = navigationContext.Parameters.GetValue<string>("Color");
|
||
}
|
||
#endregion
|
||
}
|
||
} |