完成添加变量功能
This commit is contained in:
14
ViewModels/Dialogs/VarDataDialogViewModel.cs
Normal file
14
ViewModels/Dialogs/VarDataDialogViewModel.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using PMSWPF.Models;
|
||||
|
||||
namespace PMSWPF.ViewModels.Dialogs;
|
||||
|
||||
public partial class VarDataDialogViewModel : ObservableObject
|
||||
{
|
||||
[ObservableProperty]
|
||||
private VariableData variableData;
|
||||
[ObservableProperty]
|
||||
private string title;
|
||||
[ObservableProperty]
|
||||
private string primaryButtonText;
|
||||
}
|
||||
@@ -116,9 +116,10 @@ public partial class MainViewModel : ViewModelBase
|
||||
NotificationHelper.ShowMessage("操作失败:无法获取有效的设备信息。", NotificationType.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// 2. 显示添加变量表对话框
|
||||
var varTable = await _dialogService.ShowAddVarTableDialog(device);
|
||||
var varTable = await _dialogService.ShowAddVarTableDialog();
|
||||
if (varTable == null)
|
||||
{
|
||||
// 用户取消或未选择
|
||||
|
||||
@@ -1,18 +1,28 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using NLog;
|
||||
using PMSWPF.Data.Repositories;
|
||||
using PMSWPF.Enums;
|
||||
using PMSWPF.Helper;
|
||||
using PMSWPF.Models;
|
||||
using PMSWPF.Services;
|
||||
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
||||
|
||||
namespace PMSWPF.ViewModels;
|
||||
|
||||
partial class VariableTableViewModel : ViewModelBase
|
||||
{
|
||||
|
||||
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
||||
private readonly IDialogService _dialogService;
|
||||
|
||||
// private readonly ILogger<VariableTableViewModel> _logger;
|
||||
[ObservableProperty] private VariableTable variableTable;
|
||||
[ObservableProperty] private ObservableCollection<VariableData> _dataVariables;
|
||||
[ObservableProperty]
|
||||
private VariableTable variableTable;
|
||||
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<VariableData> _dataVariables;
|
||||
|
||||
/// <summary>
|
||||
/// 是否是第一次加载,防止ToggleSwitch第一次加载触发改变事件
|
||||
@@ -20,35 +30,66 @@ partial class VariableTableViewModel : ViewModelBase
|
||||
public bool IsLoadCompletion { get; set; } = false;
|
||||
|
||||
private readonly VarTableRepository _varTableRepository;
|
||||
private readonly VarDataRepository _varDataRepository;
|
||||
|
||||
public VariableTableViewModel()
|
||||
|
||||
public VariableTableViewModel(IDialogService dialogService)
|
||||
{
|
||||
_dialogService = dialogService;
|
||||
IsLoadCompletion = false;
|
||||
// _logger = logger;
|
||||
_varTableRepository = new VarTableRepository();
|
||||
_varDataRepository = new VarDataRepository();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public override void OnLoaded()
|
||||
{
|
||||
|
||||
if (VariableTable.DataVariables != null)
|
||||
{
|
||||
DataVariables = new ObservableCollection<VariableData>(VariableTable.DataVariables);
|
||||
}
|
||||
|
||||
IsLoadCompletion = true;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public async void AddVarData(VariableTable variableTable)
|
||||
{
|
||||
try
|
||||
{
|
||||
// // 1. 显示添加设备对话框
|
||||
var varData = await _dialogService.ShowAddVarDataDialog();
|
||||
// // 如果用户取消或对话框未返回设备,则直接返回
|
||||
if (varData == null)
|
||||
{
|
||||
// _logger.LogInformation("用户取消了添加设备操作。");
|
||||
return;
|
||||
}
|
||||
|
||||
varData.VariableTableId = variableTable.Id;
|
||||
var addVarData = await _varDataRepository.AddAsync(varData);
|
||||
var msg = addVarData.Id > 0 ? $"添加变量成功:{varData?.Name}" : $"添加变量成功:{varData.Name}";
|
||||
var type = addVarData.Id > 0 ? NotificationType.Success : NotificationType.Error;
|
||||
NotificationHelper.ShowMessage(msg, type);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
string msg = $"添加变量的过程中发生了不可预期的错误:";
|
||||
Logger.Error(msg + e);
|
||||
NotificationHelper.ShowMessage(msg + e.Message, NotificationType.Error);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task OnIsActiveChanged(bool active)
|
||||
{
|
||||
|
||||
var res = await _varTableRepository.Edit(VariableTable);
|
||||
if (res > 0)
|
||||
{
|
||||
var statusMessage = active ? "已启用" : "已停用";
|
||||
NotificationHelper.ShowMessage($"变量表:{VariableTable.Name},{statusMessage}", NotificationType.Success);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -56,6 +97,4 @@ partial class VariableTableViewModel : ViewModelBase
|
||||
// _logger.LogInformation($"变量表:{VariableTable.Name},状态修改失败,状态:{active}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user