作用域配置

This commit is contained in:
czj
2026-06-05 14:29:22 +08:00
parent 347f292e9b
commit bb4fe61ded
5 changed files with 131 additions and 127 deletions

View File

@@ -1,23 +1,27 @@
using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Prism.Commands;
using Prism.Ioc;
using UIShare.GlobalVariable;
using UIShare.PubEvent;
using UIShare.UIViewModel;
using UIShare.ViewModelBase;
namespace SettingModule.ViewModels
{
public class SettingViewModel : NavigateViewModelBase, IRegionMemberLifetime, IDisposable
{
#region
public bool KeepAlive => true;
private string _testStatus = string.Empty;
public string TestStatus
{
get => _testStatus;
set => SetProperty(ref _testStatus, value);
}
private DeviceInfoModel? _selectedDevice;
public DeviceInfoModel? SelectedDevice
{
get => _selectedDevice;
@@ -29,26 +33,23 @@ namespace SettingModule.ViewModels
}
}
}
private ObservableCollection<DeviceInfoModel> _deviceList;
public ObservableCollection<DeviceInfoModel> DeviceList
{
get { return _deviceList; }
set { SetProperty(ref _deviceList,value); }
get => _deviceList;
set => SetProperty(ref _deviceList, value);
}
private string _statusMessage = "请在左侧选择设备查看 / 编辑配置";
public string StatusMessage
{
get => _statusMessage;
set => SetProperty(ref _statusMessage, value);
}
#endregion
#region
private readonly IScopedProvider _scope;
private readonly SystemConfig _systemConfig;
private ScopedContext _scopedContext { get; }
private GlobalInfo GlobalInfoRef { get; }
public ObservableCollection<string> ConnectionTypes { get; } = new()
{
"None", "TCP", "Serial"
};
#endregion
#region
@@ -57,32 +58,25 @@ namespace SettingModule.ViewModels
public ICommand ResetCommand { get; }
public ICommand OpenConnectionConfigCommand { get; }
#endregion
public ObservableCollection<string> ConnectionTypes { get; } = new()
{
"None", "TCP", "Serial"
};
#region
private IScopedProvider _scope;
private SystemConfig _systemConfig;
private ScopedContext _scopedContext { get; set; }
private GlobalInfo _globalInfo { get; }
private bool IsInitiated = false;
private string _testStatus = string.Empty;
private DeviceInfoModel? _selectedDevice;
private ObservableCollection<DeviceInfoModel> _deviceList;
private string _statusMessage = "请在左侧选择设备查看 / 编辑配置";
#endregion
public SettingViewModel(IContainerExtension container) : base(container)
{
_scope = container.CreateScope();
GlobalInfoRef = container.Resolve<GlobalInfo>();
_scopedContext = _scope.Resolve<ScopedContext>();
_systemConfig = _scope.Resolve<SystemConfig>();
// 直接复用 SystemConfig 的 DeviceList外部修改可同步反映
DeviceList = _systemConfig.DeviceList;
_globalInfo = container.Resolve<GlobalInfo>();
RefreshCommand = new DelegateCommand(OnExpand);
SaveCommand = new DelegateCommand(OnSave);
ResetCommand = new DelegateCommand(OnReset);
OpenConnectionConfigCommand = new DelegateCommand(OnOpenConnectionConfig);
// 进来默认选中第一个,让右侧不为空
if (DeviceList.Count > 0)
{
SelectedDevice = DeviceList[0];
}
}
public void Dispose()
@@ -90,7 +84,7 @@ namespace SettingModule.ViewModels
_scope?.Dispose();
}
#region
#region
/// <summary>
/// 选中设备切换:右侧配置面板靠 SelectedDevice 绑定自动刷新,
/// 这里只负责更新状态栏提示。
@@ -109,8 +103,8 @@ namespace SettingModule.ViewModels
private void OnExpand()
{
if (string.IsNullOrEmpty(TestStatus)) return;
_globalInfo.CurrentScope = TestStatus;
_eventAggregator.GetEvent<ExpandViewEvent>().Publish(TestStatus);
GlobalInfoRef.CurrentScope = TestStatus;
}
/// <summary>保存当前选中设备的配置(占位,实际写盘逻辑后续接入)。</summary>
@@ -159,7 +153,13 @@ namespace SettingModule.ViewModels
return;
}
var p = new DialogParameters { { "Device", SelectedDevice } };
// 【关键修改】在此处将选中的设备以及隔离作用域内的 _systemConfig 一并传进弹窗
var p = new DialogParameters
{
{ "Device", SelectedDevice },
{ "SystemConfig", _systemConfig }
};
_dialogService.ShowDialog(dialogName, p, r =>
{
if (r.Result == ButtonResult.OK)
@@ -170,15 +170,25 @@ namespace SettingModule.ViewModels
}
#endregion
#region
#region
public override void OnNavigatedTo(NavigationContext navigationContext)
{
base.OnNavigatedTo(navigationContext);
if (navigationContext.Parameters.ContainsKey("Name"))
if (!IsInitiated && navigationContext.Parameters.ContainsKey("Name"))
{
TestStatus = navigationContext.Parameters.GetValue<string>("Name");
_scope = _globalInfo.ScopeDic[TestStatus];
_scopedContext = _globalInfo.ContextDic[TestStatus];
_systemConfig = _scope.Resolve<SystemConfig>();
DeviceList = _systemConfig.DeviceList;
if (DeviceList != null && DeviceList.Count > 0)
{
SelectedDevice = DeviceList[0];
}
IsInitiated = true;
}
}
#endregion
}
}
}