完成变量列表的变量显示
This commit is contained in:
@@ -37,16 +37,16 @@ public class VariableData
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string Description { get; set; } = String.Empty;
|
public string Description { get; set; } = String.Empty;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 变量经过转换或格式化后的显示值。
|
|
||||||
/// </summary>
|
|
||||||
public string DisplayValue { get; set; } = String.Empty;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 变量唯一标识符。
|
/// 变量唯一标识符。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 变量经过转换或格式化后的显示值。
|
||||||
|
/// </summary>
|
||||||
|
public string DisplayValue { get; set; } = String.Empty;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 指示是否需要对变量进行报警监测。
|
/// 指示是否需要对变量进行报警监测。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using System.Collections.ObjectModel;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
using PMSWPF.Enums;
|
using PMSWPF.Enums;
|
||||||
|
|
||||||
namespace PMSWPF.Models;
|
namespace PMSWPF.Models;
|
||||||
@@ -27,7 +29,17 @@ public partial class VariableTable : ObservableObject
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 变量表中包含的数据变量列表。
|
/// 变量表中包含的数据变量列表。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public List<VariableData> DataVariables { get; set; }
|
[ObservableProperty]
|
||||||
|
public List<VariableData> dataVariables;
|
||||||
|
// [ObservableProperty]
|
||||||
|
// public ObservableCollection<VariableData> varDataList;
|
||||||
|
//
|
||||||
|
// // 列表改变了的事件
|
||||||
|
// // public event Action<List<VariableData>> OnDataVariableListChanged;
|
||||||
|
// partial void OnDataVariablesChanged(List<VariableData> variableDatas)
|
||||||
|
// {
|
||||||
|
// VarDataList=new ObservableCollection<VariableData>(variableDatas);
|
||||||
|
// }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 变量表的唯一标识符。
|
/// 变量表的唯一标识符。
|
||||||
|
|||||||
@@ -12,21 +12,37 @@ using PMSWPF.Services;
|
|||||||
|
|
||||||
namespace PMSWPF.ViewModels;
|
namespace PMSWPF.ViewModels;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 主视图模型,负责应用程序的主导航和数据管理。
|
||||||
|
/// </summary>
|
||||||
public partial class MainViewModel : ViewModelBase
|
public partial class MainViewModel : ViewModelBase
|
||||||
{
|
{
|
||||||
private readonly NavgatorServices _navgatorServices;
|
|
||||||
private readonly DataServices _dataServices;
|
private readonly DataServices _dataServices;
|
||||||
private readonly IDialogService _dialogService;
|
private readonly IDialogService _dialogService;
|
||||||
private readonly ILogger<MainViewModel> _logger;
|
private readonly ILogger<MainViewModel> _logger;
|
||||||
|
|
||||||
|
|
||||||
[ObservableProperty] private ViewModelBase currentViewModel;
|
|
||||||
[ObservableProperty] private ObservableCollection<MenuBean> _menus;
|
|
||||||
|
|
||||||
private readonly MenuRepository _menuRepository;
|
private readonly MenuRepository _menuRepository;
|
||||||
|
private readonly NavgatorServices _navgatorServices;
|
||||||
private readonly VarTableRepository _varTableRepository;
|
private readonly VarTableRepository _varTableRepository;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 当前显示的视图模型。
|
||||||
|
/// </summary>
|
||||||
|
[ObservableProperty]
|
||||||
|
private ViewModelBase currentViewModel;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 应用程序的菜单列表。
|
||||||
|
/// </summary>
|
||||||
|
[ObservableProperty]
|
||||||
|
private ObservableCollection<MenuBean> _menus;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 初始化 <see cref="MainViewModel"/> 类的新实例。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="navgatorServices">导航服务。</param>
|
||||||
|
/// <param name="dataServices">数据服务。</param>
|
||||||
|
/// <param name="dialogService">对话框服务。</param>
|
||||||
|
/// <param name="logger">日志记录器。</param>
|
||||||
public MainViewModel(NavgatorServices navgatorServices, DataServices dataServices, IDialogService dialogService,
|
public MainViewModel(NavgatorServices navgatorServices, DataServices dataServices, IDialogService dialogService,
|
||||||
ILogger<MainViewModel> logger)
|
ILogger<MainViewModel> logger)
|
||||||
{
|
{
|
||||||
@@ -35,7 +51,7 @@ public partial class MainViewModel : ViewModelBase
|
|||||||
_dialogService = dialogService;
|
_dialogService = dialogService;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_varTableRepository = new VarTableRepository();
|
_varTableRepository = new VarTableRepository();
|
||||||
_menuRepository= new MenuRepository();
|
_menuRepository = new MenuRepository();
|
||||||
|
|
||||||
_navgatorServices.OnViewModelChanged += () => { CurrentViewModel = _navgatorServices.CurrentViewModel; };
|
_navgatorServices.OnViewModelChanged += () => { CurrentViewModel = _navgatorServices.CurrentViewModel; };
|
||||||
|
|
||||||
@@ -48,63 +64,9 @@ public partial class MainViewModel : ViewModelBase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 菜单点击切换
|
/// 添加变量表。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="menu"></param>
|
/// <param name="menu">当前菜单项,用于获取父级设备信息。</param>
|
||||||
public async Task MenuSelectionChanged(MenuBean menu)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
switch (menu.Type)
|
|
||||||
{
|
|
||||||
case MenuType.MainMenu:
|
|
||||||
menu.ViewModel = DataServicesHelper.GetMainViewModel(menu.Name);
|
|
||||||
break;
|
|
||||||
case MenuType.DeviceMenu:
|
|
||||||
menu.ViewModel = App.Current.Services.GetRequiredService<DeviceDetailViewModel>();
|
|
||||||
menu.Data = _dataServices.Devices.FirstOrDefault(d => d.Id == menu.DataId);
|
|
||||||
break;
|
|
||||||
case MenuType.VariableTableMenu:
|
|
||||||
|
|
||||||
VariableTableViewModel varTableVM =
|
|
||||||
App.Current.Services.GetRequiredService<VariableTableViewModel>();
|
|
||||||
varTableVM.VariableTable =
|
|
||||||
DataServicesHelper.FindVarTableForDevice(_dataServices.Devices, menu.DataId);
|
|
||||||
|
|
||||||
varTableVM.IsLoadCompletion = false;
|
|
||||||
menu.ViewModel = varTableVM;
|
|
||||||
menu.Data = varTableVM.VariableTable;
|
|
||||||
|
|
||||||
break;
|
|
||||||
case MenuType.AddVariableTableMenu:
|
|
||||||
|
|
||||||
await AddVariableTable(menu);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (menu.Type == MenuType.AddVariableTableMenu)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (menu.ViewModel != null)
|
|
||||||
{
|
|
||||||
MessageHelper.SendNavgatorMessage(menu.ViewModel);
|
|
||||||
_logger.LogInformation($"导航到:{menu.Name}");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
NotificationHelper.ShowMessage($"菜单:{menu.Name},没有对应的ViewModel.");
|
|
||||||
_logger.LogInformation($"菜单:{menu.Name},没有对应的ViewModel.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
_logger.LogError($"菜单切换是出现了错误:{e}");
|
|
||||||
NotificationHelper.ShowMessage($"菜单切换是出现了错误:{e.Message}", NotificationType.Error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private async Task AddVariableTable(MenuBean menu)
|
private async Task AddVariableTable(MenuBean menu)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -116,7 +78,7 @@ public partial class MainViewModel : ViewModelBase
|
|||||||
NotificationHelper.ShowMessage("操作失败:无法获取有效的设备信息。", NotificationType.Error);
|
NotificationHelper.ShowMessage("操作失败:无法获取有效的设备信息。", NotificationType.Error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 2. 显示添加变量表对话框
|
// 2. 显示添加变量表对话框
|
||||||
var varTable = await _dialogService.ShowAddVarTableDialog();
|
var varTable = await _dialogService.ShowAddVarTableDialog();
|
||||||
@@ -177,4 +139,60 @@ public partial class MainViewModel : ViewModelBase
|
|||||||
NotificationHelper.ShowMessage($"添加变量表时出现了错误:{e.Message}", NotificationType.Error);
|
NotificationHelper.ShowMessage($"添加变量表时出现了错误:{e.Message}", NotificationType.Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 处理菜单选择变化的逻辑。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="menu">被选中的菜单项。</param>
|
||||||
|
public async Task MenuSelectionChanged(MenuBean menu)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
switch (menu.Type)
|
||||||
|
{
|
||||||
|
case MenuType.MainMenu:
|
||||||
|
menu.ViewModel = DataServicesHelper.GetMainViewModel(menu.Name);
|
||||||
|
break;
|
||||||
|
case MenuType.DeviceMenu:
|
||||||
|
menu.ViewModel = App.Current.Services.GetRequiredService<DeviceDetailViewModel>();
|
||||||
|
menu.Data = _dataServices.Devices.FirstOrDefault(d => d.Id == menu.DataId);
|
||||||
|
break;
|
||||||
|
case MenuType.VariableTableMenu:
|
||||||
|
|
||||||
|
VariableTableViewModel varTableVM =
|
||||||
|
App.Current.Services.GetRequiredService<VariableTableViewModel>();
|
||||||
|
varTableVM.VariableTable =
|
||||||
|
DataServicesHelper.FindVarTableForDevice(_dataServices.Devices, menu.DataId);
|
||||||
|
|
||||||
|
varTableVM.IsLoadCompletion = false;
|
||||||
|
menu.ViewModel = varTableVM;
|
||||||
|
menu.Data = varTableVM.VariableTable;
|
||||||
|
|
||||||
|
break;
|
||||||
|
case MenuType.AddVariableTableMenu:
|
||||||
|
|
||||||
|
await AddVariableTable(menu);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (menu.Type == MenuType.AddVariableTableMenu)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (menu.ViewModel != null)
|
||||||
|
{
|
||||||
|
MessageHelper.SendNavgatorMessage(menu.ViewModel);
|
||||||
|
_logger.LogInformation($"导航到:{menu.Name}");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
NotificationHelper.ShowMessage($"菜单:{menu.Name},没有对应的ViewModel.");
|
||||||
|
_logger.LogInformation($"菜单:{menu.Name},没有对应的ViewModel.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.LogError($"菜单切换是出现了错误:{e}");
|
||||||
|
NotificationHelper.ShowMessage($"菜单切换是出现了错误:{e.Message}", NotificationType.Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -13,7 +13,6 @@ namespace PMSWPF.ViewModels;
|
|||||||
|
|
||||||
partial class VariableTableViewModel : ViewModelBase
|
partial class VariableTableViewModel : ViewModelBase
|
||||||
{
|
{
|
||||||
|
|
||||||
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
||||||
private readonly IDialogService _dialogService;
|
private readonly IDialogService _dialogService;
|
||||||
|
|
||||||
@@ -50,6 +49,7 @@ partial class VariableTableViewModel : ViewModelBase
|
|||||||
DataVariables = new ObservableCollection<VariableData>(VariableTable.DataVariables);
|
DataVariables = new ObservableCollection<VariableData>(VariableTable.DataVariables);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
IsLoadCompletion = true;
|
IsLoadCompletion = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,13 +62,12 @@ partial class VariableTableViewModel : ViewModelBase
|
|||||||
var varData = await _dialogService.ShowAddVarDataDialog();
|
var varData = await _dialogService.ShowAddVarDataDialog();
|
||||||
// // 如果用户取消或对话框未返回设备,则直接返回
|
// // 如果用户取消或对话框未返回设备,则直接返回
|
||||||
if (varData == null)
|
if (varData == null)
|
||||||
{
|
|
||||||
// _logger.LogInformation("用户取消了添加设备操作。");
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
varData.VariableTableId = variableTable.Id;
|
varData.VariableTableId = variableTable.Id;
|
||||||
var addVarData = await _varDataRepository.AddAsync(varData);
|
var addVarData = await _varDataRepository.AddAsync(varData);
|
||||||
|
DataVariables?.Add(addVarData);
|
||||||
|
variableTable.DataVariables?.Add(addVarData);
|
||||||
var msg = addVarData.Id > 0 ? $"添加变量成功:{varData?.Name}" : $"添加变量成功:{varData.Name}";
|
var msg = addVarData.Id > 0 ? $"添加变量成功:{varData?.Name}" : $"添加变量成功:{varData.Name}";
|
||||||
var type = addVarData.Id > 0 ? NotificationType.Success : NotificationType.Error;
|
var type = addVarData.Id > 0 ? NotificationType.Success : NotificationType.Error;
|
||||||
NotificationHelper.ShowMessage(msg, type);
|
NotificationHelper.ShowMessage(msg, type);
|
||||||
@@ -78,7 +77,6 @@ partial class VariableTableViewModel : ViewModelBase
|
|||||||
string msg = $"添加变量的过程中发生了不可预期的错误:";
|
string msg = $"添加变量的过程中发生了不可预期的错误:";
|
||||||
Logger.Error(msg + e);
|
Logger.Error(msg + e);
|
||||||
NotificationHelper.ShowMessage(msg + e.Message, NotificationType.Error);
|
NotificationHelper.ShowMessage(msg + e.Message, NotificationType.Error);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,8 +45,7 @@
|
|||||||
<controls:CommandBar x:Name="PrimaryCommandBar"
|
<controls:CommandBar x:Name="PrimaryCommandBar"
|
||||||
DefaultLabelPosition="Right"
|
DefaultLabelPosition="Right"
|
||||||
IsOpen="False">
|
IsOpen="False">
|
||||||
<ui:AppBarButton
|
<ui:AppBarButton Command="{Binding AddVarDataCommand}"
|
||||||
Command="{Binding AddVarDataCommand}"
|
|
||||||
CommandParameter="{Binding VariableTable}"
|
CommandParameter="{Binding VariableTable}"
|
||||||
Label="添加变量">
|
Label="添加变量">
|
||||||
<ui:AppBarButton.Icon>
|
<ui:AppBarButton.Icon>
|
||||||
@@ -76,12 +75,11 @@
|
|||||||
<ikw:SimpleStackPanel Margin="5"
|
<ikw:SimpleStackPanel Margin="5"
|
||||||
Orientation="Horizontal"
|
Orientation="Horizontal"
|
||||||
Spacing="10">
|
Spacing="10">
|
||||||
<ui:ToggleSwitch
|
<ui:ToggleSwitch OnContent="启用"
|
||||||
OnContent="启用"
|
OffContent="停用"
|
||||||
OffContent="停用"
|
Toggled="OnIsActiveChanged"
|
||||||
Toggled="OnIsActiveChanged"
|
FontSize="16"
|
||||||
FontSize="16"
|
IsOn="{Binding VariableTable.IsActive}" />
|
||||||
IsOn="{Binding VariableTable.IsActive}"/>
|
|
||||||
<TextBlock Style="{StaticResource VarTableLabelStyle}"
|
<TextBlock Style="{StaticResource VarTableLabelStyle}"
|
||||||
Text="变量表名称:" />
|
Text="变量表名称:" />
|
||||||
<TextBlock Style="{StaticResource VarTableValueStyle}"
|
<TextBlock Style="{StaticResource VarTableValueStyle}"
|
||||||
@@ -94,14 +92,36 @@
|
|||||||
Text="所属设备:" />
|
Text="所属设备:" />
|
||||||
<TextBlock Style="{StaticResource VarTableValueStyle}"
|
<TextBlock Style="{StaticResource VarTableValueStyle}"
|
||||||
Text="{Binding VariableTable.Device.Name}" />
|
Text="{Binding VariableTable.Device.Name}" />
|
||||||
|
|
||||||
|
|
||||||
</ikw:SimpleStackPanel>
|
</ikw:SimpleStackPanel>
|
||||||
|
|
||||||
|
|
||||||
</ikw:SimpleStackPanel>
|
</ikw:SimpleStackPanel>
|
||||||
|
|
||||||
<DataGrid Margin="10" />
|
<DataGrid Margin="10"
|
||||||
|
AutoGenerateColumns="False"
|
||||||
|
CanUserSortColumns="True"
|
||||||
|
ItemsSource="{Binding DataVariables}">
|
||||||
|
<DataGrid.Columns>
|
||||||
|
<DataGridTextColumn Header="名称" Binding="{Binding Name}" />
|
||||||
|
<DataGridTextColumn Header="描述" Binding="{Binding Description}" />
|
||||||
|
<DataGridTextColumn Header="节点ID" Binding="{Binding NodeId}" />
|
||||||
|
<DataGridTextColumn Header="协议类型" Binding="{Binding ProtocolType}" />
|
||||||
|
<DataGridTextColumn Header="数据类型" Binding="{Binding DataType}" />
|
||||||
|
<DataGridTextColumn Header="信号类型" Binding="{Binding SignalType}" />
|
||||||
|
<DataGridTextColumn Header="当前值" Binding="{Binding DataValue}" />
|
||||||
|
<DataGridTextColumn Header="显示值" Binding="{Binding DisplayValue}" />
|
||||||
|
<DataGridTextColumn Header="更新时间" Binding="{Binding UpdateTime}" />
|
||||||
|
<DataGridTextColumn Header="转换规则" Binding="{Binding Converstion}" />
|
||||||
|
<DataGridCheckBoxColumn Header="启用" Binding="{Binding IsActive}" />
|
||||||
|
<DataGridCheckBoxColumn Header="保存" Binding="{Binding IsSave}" />
|
||||||
|
<DataGridTextColumn Header="保存范围" Binding="{Binding SaveRange}" />
|
||||||
|
<DataGridCheckBoxColumn Header="报警" Binding="{Binding IsAlarm}" />
|
||||||
|
<DataGridTextColumn Header="报警最小值" Binding="{Binding AlarmMin}" />
|
||||||
|
<DataGridTextColumn Header="报警最大值" Binding="{Binding AlarmMax}" />
|
||||||
|
</DataGrid.Columns>
|
||||||
|
</DataGrid>
|
||||||
</DockPanel>
|
</DockPanel>
|
||||||
|
|
||||||
</UserControl>
|
</UserControl>
|
||||||
Reference in New Issue
Block a user