diff --git a/App.xaml.cs b/App.xaml.cs index f375578..e39c707 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -50,7 +50,7 @@ public partial class App : Application container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); - container.AddSingleton(); + container.AddTransient(); container.AddSingleton(); container.AddScoped(); container.AddScoped(); diff --git a/Data/Entities/DbVariableTable.cs b/Data/Entities/DbVariableTable.cs index d6d8a00..9048347 100644 --- a/Data/Entities/DbVariableTable.cs +++ b/Data/Entities/DbVariableTable.cs @@ -12,6 +12,7 @@ public class DbVariableTable public string Name { get; set; } + public bool IsActive { get; set; } [SugarColumn(IsNullable = true)] public string? Description { get; set; } [SugarColumn(ColumnDataType = "varchar(20)", SqlParameterDbType = typeof(EnumToStringConvert))] diff --git a/Data/Repositories/VarTableRepository.cs b/Data/Repositories/VarTableRepository.cs index 3a2eb46..692c8bd 100644 --- a/Data/Repositories/VarTableRepository.cs +++ b/Data/Repositories/VarTableRepository.cs @@ -18,4 +18,12 @@ public class VarTableRepository return await db.Insertable(varTable.CopyTo()).ExecuteReturnIdentityAsync(); } } + + public async Task Edit(VariableTable variableTable) + { + using (var db = DbContext.GetInstance()) + { + return await db.Updateable(variableTable.CopyTo()).ExecuteCommandAsync(); + } + } } \ No newline at end of file diff --git a/Extensions/ObjectExtensions.cs b/Extensions/ObjectExtensions.cs index 605c887..e7bd383 100644 --- a/Extensions/ObjectExtensions.cs +++ b/Extensions/ObjectExtensions.cs @@ -76,16 +76,26 @@ public static class ObjectExtensions targetProperty.SetValue(ttarget, sourceValue); } // 场景 2: 属性类型不同,但可能是泛型 List 类型 - else if(isTargetList && isSourceList) + else if (isTargetList && isSourceList) { CopyGenericList(ttarget, sourceProperty, targetProperty, sourceValue); } + // 场景 3: 属性类型不同,但是属性名称一样 else { var sObj = sourceProperty.GetValue(tsource); + if (sObj == null) + { + continue; + } var tObj = targetProperty.GetValue(ttarget); - // if (sObj == null && tObj == null) - // CopyTo(sObj,tObj); + if (tObj == null) + { + tObj=Activator.CreateInstance(targetProperty.PropertyType); + } + + CopyTo(sObj,tObj); + targetProperty.SetValue(ttarget,tObj); } } } diff --git a/Helper/MessageHelper.cs b/Helper/MessageHelper.cs index 512f7ca..8017083 100644 --- a/Helper/MessageHelper.cs +++ b/Helper/MessageHelper.cs @@ -24,11 +24,8 @@ public class MessageHelper /// /// 导航View的ViewModel /// 带的参数 - public static void SendNavgatorMessage(ViewModelBase vm,Object param=null) + public static void SendNavgatorMessage(ViewModelBase vm) { - WeakReferenceMessenger.Default.Send(new NavgatorMessage(vm) - { - Parameters = param - }); + WeakReferenceMessenger.Default.Send(new NavgatorMessage(vm)); } } \ No newline at end of file diff --git a/Models/VariableTable.cs b/Models/VariableTable.cs index 2e96e0e..4535999 100644 --- a/Models/VariableTable.cs +++ b/Models/VariableTable.cs @@ -11,7 +11,8 @@ public partial class VariableTable:ObservableObject public ProtocolType ProtocolType { get; set; } public List DataVariables { get; set; } - + [ObservableProperty] + private bool isActive; public int? DeviceId { get; set; } public Device? Device { get; set; } } \ No newline at end of file diff --git a/Services/NavgatorServices.cs b/Services/NavgatorServices.cs index 0d91141..b53bedf 100644 --- a/Services/NavgatorServices.cs +++ b/Services/NavgatorServices.cs @@ -1,42 +1,62 @@ using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Messaging; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using PMSWPF.Enums; +using PMSWPF.Helper; using PMSWPF.Message; using PMSWPF.ViewModels; namespace PMSWPF.Services; -public class NavgatorServices : ObservableRecipient, IRecipient +public partial class NavgatorServices : ObservableRecipient, IRecipient { - private ViewModelBase currentViewModel; + private readonly ILogger _logger; - public NavgatorServices() + [ObservableProperty] private ViewModelBase currentViewModel; + + public NavgatorServices(ILogger logger) { + _logger = logger; IsActive = true; } - public ViewModelBase CurrentViewModel + partial void OnCurrentViewModelChanging(ViewModelBase viewModel) { - get => currentViewModel; - set - { - currentViewModel = value; - OnViewModelChanged?.Invoke(); - currentViewModel.OnLoaded(); - } + viewModel?.OnLoading(); } + partial void OnCurrentViewModelChanged(ViewModelBase viewModel) + { + OnViewModelChanged?.Invoke(); + viewModel?.OnLoaded(); + } + + // public ViewModelBase CurrentViewModel + // { + // get => currentViewModel; + // set + // { + // value?.OnLoading(); + // currentViewModel = value; + // OnViewModelChanged?.Invoke(); + // currentViewModel?.OnLoaded(); + // } + // } + + public void Receive(NavgatorMessage message) { - CurrentViewModel = message.Value; - CurrentViewModel.NavgateParameters = message.Parameters; + try + { + CurrentViewModel = message.Value; + } + catch (Exception e) + { + NotificationHelper.ShowMessage($"切换视图时发生了错误:{e.Message}", NotificationType.Error); + _logger.LogError($"切换视图时发生了错误:{e}"); + } } public event Action OnViewModelChanged; - - public void NavigateTo() where T : ViewModelBase - { - // Application.Current - CurrentViewModel = App.Current.Services.GetService(); - } } \ No newline at end of file diff --git a/ViewModels/DataTransformViewModel.cs b/ViewModels/DataTransformViewModel.cs index 3182508..59d1b36 100644 --- a/ViewModels/DataTransformViewModel.cs +++ b/ViewModels/DataTransformViewModel.cs @@ -2,7 +2,5 @@ public class DataTransformViewModel : ViewModelBase { - public override void OnLoaded() - { - } + } \ No newline at end of file diff --git a/ViewModels/DeviceDetailViewModel.cs b/ViewModels/DeviceDetailViewModel.cs index f53a173..3ccc9db 100644 --- a/ViewModels/DeviceDetailViewModel.cs +++ b/ViewModels/DeviceDetailViewModel.cs @@ -2,8 +2,5 @@ public class DeviceDetailViewModel:ViewModelBase { - public override void OnLoaded() - { - - } + } \ No newline at end of file diff --git a/ViewModels/DevicesViewModel.cs b/ViewModels/DevicesViewModel.cs index 97756da..4601660 100644 --- a/ViewModels/DevicesViewModel.cs +++ b/ViewModels/DevicesViewModel.cs @@ -36,54 +36,135 @@ public partial class DevicesViewModel : ViewModelBase _dataServices.OnDeviceListChanged += (devices) => { Devices = new ObservableCollection(devices); }; } + // /// + // /// 添加设备 + // /// + // [RelayCommand] + // public async void AddDevice() + // { + // Device device = null; + // try + // { + // device = await _dialogService.ShowAddDeviceDialog(); + // if (device != null) + // { + // device = await _deviceRepository.Add(device); + // if (device != null) + // { + // var msg = $"添加设备成功:{device.Name}"; + // _logger.LogInformation(msg); + // + // bool addMenuRes = await _menuRepository.AddDeviceMenu(device); + // if (addMenuRes) + // { + // // 通知更新菜单 + // MessageHelper.SendLoadMessage(LoadTypes.Menu); + // MessageHelper.SendLoadMessage(LoadTypes.Devices); + // NotificationHelper.ShowMessage(msg, NotificationType.Success); + // } + // else + // { + // var msgerr = $"给设备添加菜单失败:{device.Name}"; + // _logger.LogInformation(msgerr); + // NotificationHelper.ShowMessage(msgerr, NotificationType.Error); + // } + // } + // else + // { + // var msg = $"添加设备失败:{device.Name}"; + // _logger.LogInformation(msg); + // NotificationHelper.ShowMessage(msg, NotificationType.Error); + // } + // } + // } + // catch (Exception e) + // { + // var msg = $"添加设备失败:{e.Message}"; + // _logger.LogError(msg); + // NotificationHelper.ShowMessage(msg, NotificationType.Error); + // } + // } + /// - /// 添加设备 - /// - [RelayCommand] - public async void AddDevice() - { - Device device = null; - try - { - device = await _dialogService.ShowAddDeviceDialog(); - if (device != null) - { - device = await _deviceRepository.Add(device); - if (device != null) - { - var msg = $"添加设备成功:{device.Name}"; - _logger.LogInformation(msg); +/// 辅助方法:处理成功通知和日志 +/// +private void HandleOperationSuccess(string entityType, string operation, string entityName) +{ + var message = $"{entityType}{operation}成功:{entityName}"; + _logger.LogInformation(message); + NotificationHelper.ShowMessage(message, NotificationType.Success); +} - bool addMenuRes = await _menuRepository.AddDeviceMenu(device); - if (addMenuRes) - { - // 通知更新菜单 - MessageHelper.SendLoadMessage(LoadTypes.Menu); - MessageHelper.SendLoadMessage(LoadTypes.Devices); - NotificationHelper.ShowMessage(msg, NotificationType.Success); - } - else - { - var msgerr = $"给设备添加菜单失败:{device.Name}"; - _logger.LogInformation(msgerr); - NotificationHelper.ShowMessage(msgerr, NotificationType.Error); - } - } - else - { - var msg = $"添加设备失败:{device.Name}"; - _logger.LogInformation(msg); - NotificationHelper.ShowMessage(msg, NotificationType.Error); - } - } - } - catch (Exception e) +/// +/// 辅助方法:处理失败通知和日志 +/// +private void HandleOperationFailure(string entityType, string operation, string entityName, Exception ex = null) +{ + var message = $"{entityType}{operation}失败:{entityName}"; + if (ex != null) + { + _logger.LogError(ex, message); + } + else + { + _logger.LogError(message); + } + NotificationHelper.ShowMessage(message, NotificationType.Error); +} + +/// +/// 添加设备 +/// +[RelayCommand] +public async void AddDevice() +{ + try + { + // 1. 显示添加设备对话框 + var device = await _dialogService.ShowAddDeviceDialog(); + // 如果用户取消或对话框未返回设备,则直接返回 + if (device == null) { - var msg = $"添加设备失败:{e.Message}"; - _logger.LogError(msg); - NotificationHelper.ShowMessage(msg, NotificationType.Error); + _logger.LogInformation("用户取消了添加设备操作。"); + return; + } + + // 2. 将设备添加到数据库 + var addedDevice = await _deviceRepository.Add(device); + // 如果数据库添加失败 + if (addedDevice == null) + { + HandleOperationFailure("设备", "添加", device.Name); + return; // 提前返回 + } + + // 3. 设备成功添加到数据库,进行菜单添加 + // 这里立即发出成功的通知和日志 + HandleOperationSuccess("设备", "添加", addedDevice.Name); + + // 4. 为新设备添加菜单 + bool menuAddedSuccessfully = await _menuRepository.AddDeviceMenu(addedDevice); + if (menuAddedSuccessfully) + { + // 菜单也添加成功,通知 UI 更新 + MessageHelper.SendLoadMessage(LoadTypes.Menu); + MessageHelper.SendLoadMessage(LoadTypes.Devices); + } + else + { + // 菜单添加失败,通知用户并记录日志 + HandleOperationFailure("设备", "添加菜单", addedDevice.Name); + // 考虑:如果菜单添加失败,是否需要回滚设备添加? + // 例如:await _deviceRepository.Delete(addedDevice.Id); } } + catch (Exception e) + { + // 捕获并记录所有未预期的异常 + _logger.LogError(e, "在添加设备过程中发生未预期错误。"); + NotificationHelper.ShowMessage($"添加设备失败:{e.Message}", NotificationType.Error); + } +} /// @@ -153,14 +234,8 @@ public partial class DevicesViewModel : ViewModelBase _logger.LogError($"删除设备的过程中发生错误:{e}"); } } - - [RelayCommand] - public void NavigateVt() - { - } - public override async void OnLoaded() - { - } + + } \ No newline at end of file diff --git a/ViewModels/HomeViewModel.cs b/ViewModels/HomeViewModel.cs index ad95147..94bb2ae 100644 --- a/ViewModels/HomeViewModel.cs +++ b/ViewModels/HomeViewModel.cs @@ -2,7 +2,6 @@ public class HomeViewModel : ViewModelBase { - public override void OnLoaded() - { - } + + } \ No newline at end of file diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs index c377293..c5ca0fe 100644 --- a/ViewModels/MainViewModel.cs +++ b/ViewModels/MainViewModel.cs @@ -24,6 +24,7 @@ public partial class MainViewModel : ViewModelBase [ObservableProperty] private ObservableCollection _menus; private readonly MenuRepository _menuRepository; + private readonly VarTableRepository _varTableRepository; public MainViewModel(NavgatorServices navgatorServices, DataServices dataServices, IDialogService dialogService, @@ -33,7 +34,8 @@ public partial class MainViewModel : ViewModelBase _dataServices = dataServices; _dialogService = dialogService; _logger = logger; - + _varTableRepository = new VarTableRepository(); + _menuRepository= new MenuRepository(); _navgatorServices.OnViewModelChanged += () => { CurrentViewModel = _navgatorServices.CurrentViewModel; }; @@ -45,6 +47,10 @@ public partial class MainViewModel : ViewModelBase dataServices.OnMenuTreeListChanged += (menus) => { Menus = new ObservableCollection(menus); }; } + /// + /// 菜单点击切换 + /// + /// public async Task MenuSelectionChanged(MenuBean menu) { try @@ -60,19 +66,22 @@ public partial class MainViewModel : ViewModelBase break; case MenuType.VariableTableMenu: - VariableTableViewModel varTableVM = App.Current.Services.GetRequiredService(); + VariableTableViewModel varTableVM = + App.Current.Services.GetRequiredService(); 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) + if (menu.Type == MenuType.AddVariableTableMenu) return; if (menu.ViewModel != null) @@ -93,66 +102,77 @@ public partial class MainViewModel : ViewModelBase } } + private async Task AddVariableTable(MenuBean menu) { try { - if (menu.Parent != null && menu.Parent.Data != null) + // 1. 检查父级设备信息 + if (menu.Parent?.Data is not Device device) { - Device device = (Device)menu.Parent.Data; - var varTable = await _dialogService.ShowAddVarTableDialog(device); - if (varTable != null) - { - // 添加变量表 - varTable.DeviceId = device.Id; - varTable.ProtocolType = device.ProtocolType; - var addVarTableId = await new VarTableRepository().Add(varTable); - if (addVarTableId > 0) - { - // 添加变量表菜单 - MenuBean newMenu = new MenuBean(); - newMenu.Icon = SegoeFluentIcons.Tablet.Glyph; - newMenu.Name = varTable.Name; - newMenu.DataId = addVarTableId; - newMenu.Type = MenuType.VariableTableMenu; - newMenu.ParentId = menu.Parent.Id; - var addMenuRes = await new MenuRepository().Add(newMenu); - if (addMenuRes > 0) - { - // 变量表菜单添加成功 - MessageHelper.SendLoadMessage(LoadTypes.Menu); - MessageHelper.SendLoadMessage(LoadTypes.Devices); - NotificationHelper.ShowMessage($"变量表:{varTable.Name},添加成功", - NotificationType.Success); - _logger.LogInformation($"变量表:{varTable.Name},添加成功"); - } - else - { - // 变量表菜单添加失败 - NotificationHelper.ShowMessage($"变量表:{varTable.Name},添加菜单失败", - NotificationType.Error); - _logger.LogError($"变量表:{varTable.Name},添加菜单失败"); - } - } - else - { - // 变量表添加失败 - NotificationHelper.ShowMessage($"变量表:{varTable.Name},添加失败", NotificationType.Error); - _logger.LogError($"变量表:{varTable.Name},添加失败"); - } - } + _logger.LogWarning("尝试添加变量表时,Parent 或 Parent.Data 为空,或 Parent.Data 不是 Device 类型。"); + NotificationHelper.ShowMessage("操作失败:无法获取有效的设备信息。", NotificationType.Error); + return; + } + + // 2. 显示添加变量表对话框 + var varTable = await _dialogService.ShowAddVarTableDialog(device); + if (varTable == null) + { + // 用户取消或未选择 + return; + } + + // 3. 设置变量表属性 + varTable.IsActive = true; + varTable.DeviceId = device.Id; + varTable.ProtocolType = device.ProtocolType; + + // 4. 添加变量表到数据库 + // 假设 _varTableRepository.Add 返回一个布尔值表示成功,或者一个表示 ID 的整数 + // 这里为了演示,我们假设它返回新添加的ID,如果失败则返回0 + var addVarTableId = await _varTableRepository.Add(varTable); + if (addVarTableId <= 0) + { + NotificationHelper.ShowMessage($"变量表:{varTable.Name},添加失败", NotificationType.Error); + _logger.LogError($"变量表:{varTable.Name},添加失败"); + return; // 添加变量表失败,提前返回 + } + + // 5. 添加变量表菜单 + MenuBean newMenu = new MenuBean + { + Icon = SegoeFluentIcons.Tablet.Glyph, + Name = varTable.Name, + DataId = addVarTableId, // 使用实际添加的ID + Type = MenuType.VariableTableMenu, + ParentId = menu.Parent.Id + }; + + var addMenuRes = await _menuRepository.Add(newMenu); + if (addMenuRes > 0) + { + // 变量表和菜单都添加成功 + MessageHelper.SendLoadMessage(LoadTypes.Menu); + MessageHelper.SendLoadMessage(LoadTypes.Devices); + NotificationHelper.ShowMessage($"变量表:{varTable.Name},添加成功", NotificationType.Success); + _logger.LogInformation($"变量表:{varTable.Name},添加成功"); + } + else + { + // 变量表菜单添加失败 (此时变量表可能已添加成功,需要根据业务决定是否回滚) + NotificationHelper.ShowMessage($"变量表:{varTable.Name},添加菜单失败", NotificationType.Error); + _logger.LogError($"变量表:{varTable.Name},添加菜单失败"); + // 考虑:如果菜单添加失败,是否需要删除之前添加的变量表? + // 例如:await _varTableRepository.Delete(addVarTableId); } } catch (Exception e) { - _logger.LogError($"添加变量表时出现了错误:{e}"); + // 捕获并记录所有未预料的异常 + _logger.LogError(e, "添加变量表时出现了未预期的错误。"); NotificationHelper.ShowMessage($"添加变量表时出现了错误:{e.Message}", NotificationType.Error); } } - - - public override void OnLoaded() - { - } } \ No newline at end of file diff --git a/ViewModels/SettingViewModel.cs b/ViewModels/SettingViewModel.cs index b77a439..f8123fa 100644 --- a/ViewModels/SettingViewModel.cs +++ b/ViewModels/SettingViewModel.cs @@ -2,7 +2,5 @@ namespace PMSWPF.ViewModels; public class SettingViewModel : ViewModelBase { - public override void OnLoaded() - { - } + } \ No newline at end of file diff --git a/ViewModels/VariableTableViewModel.cs b/ViewModels/VariableTableViewModel.cs index 655dcb9..0f836be 100644 --- a/ViewModels/VariableTableViewModel.cs +++ b/ViewModels/VariableTableViewModel.cs @@ -1,21 +1,61 @@ using System.Collections.ObjectModel; using CommunityToolkit.Mvvm.ComponentModel; +using Microsoft.Extensions.Logging; +using PMSWPF.Data.Repositories; +using PMSWPF.Enums; +using PMSWPF.Helper; using PMSWPF.Models; namespace PMSWPF.ViewModels; partial class VariableTableViewModel : ViewModelBase { - [ObservableProperty] - private VariableTable variableTable; - [ObservableProperty] - private ObservableCollection _dataVariables; + // private readonly ILogger _logger; + [ObservableProperty] private VariableTable variableTable; + [ObservableProperty] private ObservableCollection _dataVariables; + + /// + /// 是否是第一次加载,防止ToggleSwitch第一次加载触发改变事件 + /// + public bool IsLoadCompletion { get; set; } = false; + + private readonly VarTableRepository _varTableRepository; + + public VariableTableViewModel() + { + IsLoadCompletion = false; + // _logger = logger; + _varTableRepository = new VarTableRepository(); + } + + public override void OnLoaded() { - if (VariableTable.DataVariables!=null ) + + if (VariableTable.DataVariables != null) { DataVariables = new ObservableCollection(VariableTable.DataVariables); } + IsLoadCompletion = true; } + + 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 + { + NotificationHelper.ShowMessage($"变量表:{VariableTable.Name},状态修改失败,状态:{active}", NotificationType.Error); + // _logger.LogInformation($"变量表:{VariableTable.Name},状态修改失败,状态:{active}"); + } + } + + } \ No newline at end of file diff --git a/ViewModels/ViewModelBase.cs b/ViewModels/ViewModelBase.cs index c16e622..e265141 100644 --- a/ViewModels/ViewModelBase.cs +++ b/ViewModels/ViewModelBase.cs @@ -4,6 +4,16 @@ namespace PMSWPF.ViewModels; public abstract class ViewModelBase : ObservableObject { - public Object NavgateParameters { get; set; } - public abstract void OnLoaded(); + public virtual void OnLoaded() + { + + } + + public virtual void OnLoading() + { + + } + + + } \ No newline at end of file diff --git a/Views/DevicesView.xaml b/Views/DevicesView.xaml index b7b937a..24ecca0 100644 --- a/Views/DevicesView.xaml +++ b/Views/DevicesView.xaml @@ -22,7 +22,7 @@ - @@ -50,6 +49,7 @@ MenuItemsSource="{Binding Menus}" MenuItemTemplate="{StaticResource NavigationViewMenuItem}" SelectionChanged="NavigationView_SelectionChanged"> + @@ -86,7 +86,7 @@ - + diff --git a/Views/VariableTableView.xaml b/Views/VariableTableView.xaml index 47cab11..c3fc0f5 100644 --- a/Views/VariableTableView.xaml +++ b/Views/VariableTableView.xaml @@ -8,29 +8,43 @@ xmlns:vm="clr-namespace:PMSWPF.ViewModels" xmlns:controls="http://schemas.inkore.net/lib/ui/wpf/modern" mc:Ignorable="d" + Loaded="VariableTableView_OnLoaded" d:DataContext="{d:DesignInstance vm:VariableTableViewModel}" - d:DesignHeight="600" d:DesignWidth="800"> + d:DesignHeight="600" + d:DesignWidth="800"> - - - - - + + + + DefaultLabelPosition="Right" + IsOpen="False"> @@ -53,24 +67,40 @@ + Icon="Setting" + Label="Settings" /> - - - - - - - + + + + + + + + + + - + \ No newline at end of file diff --git a/Views/VariableTableView.xaml.cs b/Views/VariableTableView.xaml.cs index 6376421..649c791 100644 --- a/Views/VariableTableView.xaml.cs +++ b/Views/VariableTableView.xaml.cs @@ -1,14 +1,44 @@ +using System.Windows; using System.Windows.Controls; +using iNKORE.UI.WPF.Modern.Controls; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using PMSWPF.Helper; using PMSWPF.ViewModels; namespace PMSWPF.Views; public partial class VariableTableView : UserControl { + public bool IsLoadCompletion; + public VariableTableView() { InitializeComponent(); - DataContext = App.Current.Services.GetRequiredService(); + IsLoadCompletion = false; + } + + + private async void OnIsActiveChanged(object sender, RoutedEventArgs e) + { + try + { + var _viewModel = (VariableTableViewModel)this.DataContext; + // 判断如果没有加载完成就跳过,防止ToggleSwtich加载的时候触发 + if (!_viewModel.IsLoadCompletion || !IsLoadCompletion) + return; + + ToggleSwitch toggleSwitch = (ToggleSwitch)sender; + await _viewModel.OnIsActiveChanged(toggleSwitch.IsOn); + } + catch (Exception exception) + { + NotificationHelper.ShowMessage($"修改变量表启用,停用时发生了错误:{exception.Message}"); + } + } + + private void VariableTableView_OnLoaded(object sender, RoutedEventArgs e) + { + IsLoadCompletion = true; } } \ No newline at end of file