完成从Excel导入变量的对话框

This commit is contained in:
2025-08-22 20:46:23 +08:00
parent f821024756
commit 7e2e01e3cd
4 changed files with 30 additions and 21 deletions

View File

@@ -22,6 +22,7 @@ using Microsoft.Extensions.Hosting;
using DMS.WPF.Helper; using DMS.WPF.Helper;
using DMS.WPF.Services; using DMS.WPF.Services;
using DMS.WPF.Services.Processors; using DMS.WPF.Services.Processors;
using DMS.WPF.ViewModels.Dialogs;
using DataProcessingService = DMS.Services.DataProcessingService; using DataProcessingService = DMS.Services.DataProcessingService;
using IDataProcessingService = DMS.Services.IDataProcessingService; using IDataProcessingService = DMS.Services.IDataProcessingService;
using LogLevel = Microsoft.Extensions.Logging.LogLevel; using LogLevel = Microsoft.Extensions.Logging.LogLevel;
@@ -179,6 +180,8 @@ public partial class App : System.Windows.Application
//services.AddScoped<MqttServerDetailViewModel>(); //services.AddScoped<MqttServerDetailViewModel>();
services.AddSingleton<DeviceDetailViewModel>(); services.AddSingleton<DeviceDetailViewModel>();
services.AddSingleton<MqttsViewModel>(); services.AddSingleton<MqttsViewModel>();
// 注册对话框模型
services.AddTransient<ImportExcelDialogViewModel>();
// 注册对话框 // 注册对话框
services.AddSingleton<DevicesView>(); services.AddSingleton<DevicesView>();
//注册View视图 //注册View视图

View File

@@ -1,4 +1,6 @@
using System.Collections;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq;
using AutoMapper; using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Input;
@@ -11,7 +13,6 @@ namespace DMS.WPF.ViewModels.Dialogs;
public partial class ImportExcelDialogViewModel : DialogViewModelBase<List<Variable>> public partial class ImportExcelDialogViewModel : DialogViewModelBase<List<Variable>>
{ {
private readonly IMapper _mapper;
private readonly IExcelService _excelService; private readonly IExcelService _excelService;
[ObservableProperty] [ObservableProperty]
@@ -19,12 +20,12 @@ public partial class ImportExcelDialogViewModel : DialogViewModelBase<List<Varia
[ObservableProperty] [ObservableProperty]
private List<Variable> _variables = new(); private List<Variable> _variables = new();
[ObservableProperty]
private IList _selectedVariables = new ArrayList();
public ImportExcelDialogViewModel(IMapper mapper,IExcelService excelService) public ImportExcelDialogViewModel(IExcelService excelService)
{ {
_mapper = mapper;
_excelService = excelService; _excelService = excelService;
} }
@@ -44,20 +45,23 @@ public partial class ImportExcelDialogViewModel : DialogViewModelBase<List<Varia
NotificationHelper.ShowError($"从Excel文件中读取变量时发生了错误:{ex.Message}",ex); NotificationHelper.ShowError($"从Excel文件中读取变量时发生了错误:{ex.Message}",ex);
} }
} }
[RelayCommand]
public void SecondaryButton()
{
}
[RelayCommand] [RelayCommand]
private void PrimaryButton() private void ImportAll()
{ {
Close(Variables); Close(Variables);
} }
[RelayCommand]
private void ImportSelected()
{
var selected = SelectedVariables.Cast<Variable>().ToList();
Close(selected);
}
[RelayCommand] [RelayCommand]
private void CancleButton() private void CancleButton()
{ {
Close(null); Close(null);
} }
} }

View File

@@ -10,6 +10,7 @@ using DMS.WPF.ViewModels.Items;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Windows.Data; using System.Windows.Data;
using Microsoft.Extensions.DependencyInjection;
namespace DMS.WPF.ViewModels; namespace DMS.WPF.ViewModels;
@@ -17,7 +18,6 @@ namespace DMS.WPF.ViewModels;
partial class VariableTableViewModel : ViewModelBase, INavigatable partial class VariableTableViewModel : ViewModelBase, INavigatable
{ {
private readonly IMapper _mapper; private readonly IMapper _mapper;
private readonly IExcelService _excelService;
/// <summary> /// <summary>
/// 对话服务接口,用于显示各种对话框(如确认、编辑、导入等)。 /// 对话服务接口,用于显示各种对话框(如确认、编辑、导入等)。
@@ -91,10 +91,9 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
/// <param name="dialogService">对话服务接口的实例。</param> /// <param name="dialogService">对话服务接口的实例。</param>
private readonly DataServices _dataServices; private readonly DataServices _dataServices;
public VariableTableViewModel(IMapper mapper, IExcelService excelService, IDialogService dialogService, DataServices dataServices) public VariableTableViewModel(IMapper mapper, IDialogService dialogService, DataServices dataServices)
{ {
_mapper = mapper; _mapper = mapper;
_excelService = excelService;
_dialogService = dialogService; _dialogService = dialogService;
_dataServices = dataServices; _dataServices = dataServices;
IsLoadCompletion = false; // 初始设置为 false表示未完成加载 IsLoadCompletion = false; // 初始设置为 false表示未完成加载
@@ -295,10 +294,10 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
[RelayCommand] [RelayCommand]
private async void ImprotFromTiaVarTable() private async void ImprotFromTiaVarTable()
{ {
ImportExcelDialogViewModel viewModel = new ImportExcelDialogViewModel(_mapper, _excelService); ImportExcelDialogViewModel viewModel = App.Current.Services.GetRequiredService<ImportExcelDialogViewModel>();
List<Variable> improtVariable = await _dialogService.ShowDialogAsync(viewModel); List<Variable> improtVariable = await _dialogService.ShowDialogAsync(viewModel);
if (improtVariable == null) return;
if (improtVariable == null || improtVariable.Count==0) return;

View File

@@ -6,12 +6,12 @@
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern" xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors" xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:vmd="clr-namespace:DMS.WPF.ViewModels.Dialogs" xmlns:vmd="clr-namespace:DMS.WPF.ViewModels.Dialogs"
xmlns:helper="clr-namespace:DMS.WPF.Helper"
Title="从Excel导入" Title="从Excel导入"
CloseButtonText="取消" CloseButtonText="取消"
DefaultButton="Primary" DefaultButton="Primary"
PrimaryButtonText="导入全部" PrimaryButtonText="导入全部"
SecondaryButtonText="导入选择" SecondaryButtonText="导入选择"
d:DataContext="{d:DesignInstance vmd:ImportExcelDialogViewModel}" d:DataContext="{d:DesignInstance vmd:ImportExcelDialogViewModel}"
mc:Ignorable="d" mc:Ignorable="d"
AllowDrop="True" AllowDrop="True"
@@ -20,10 +20,10 @@
<i:Interaction.Triggers> <i:Interaction.Triggers>
<i:EventTrigger EventName="PrimaryButtonClick"> <i:EventTrigger EventName="PrimaryButtonClick">
<i:InvokeCommandAction Command="{Binding PrimaryButtonCommand}" /> <i:InvokeCommandAction Command="{Binding ImportAllCommand}" />
</i:EventTrigger> </i:EventTrigger>
<i:EventTrigger EventName="SecondaryButtonClick"> <i:EventTrigger EventName="SecondaryButtonClick">
<i:InvokeCommandAction Command="{Binding SecondaryButtonCommand}" /> <i:InvokeCommandAction Command="{Binding ImportSelectedCommand}" />
</i:EventTrigger> </i:EventTrigger>
<i:EventTrigger EventName="CloseButtonClick"> <i:EventTrigger EventName="CloseButtonClick">
<i:InvokeCommandAction Command="{Binding CancleButtonCommand}" /> <i:InvokeCommandAction Command="{Binding CancleButtonCommand}" />
@@ -59,6 +59,9 @@
ItemsSource="{Binding Variables}" ItemsSource="{Binding Variables}"
AutoGenerateColumns="False" AutoGenerateColumns="False"
CanUserAddRows="False"> CanUserAddRows="False">
<i:Interaction.Behaviors>
<helper:SelectedItemsBehavior SelectedItems="{Binding SelectedVariables}" />
</i:Interaction.Behaviors>
<DataGrid.Style> <DataGrid.Style>
<Style BasedOn="{StaticResource {x:Type DataGrid}}" <Style BasedOn="{StaticResource {x:Type DataGrid}}"
TargetType="DataGrid"> TargetType="DataGrid">
@@ -75,7 +78,7 @@
<DataGridTextColumn Header="名称" <DataGridTextColumn Header="名称"
Binding="{Binding Name}" /> Binding="{Binding Name}" />
<DataGridTextColumn Header="数据类型" <DataGridTextColumn Header="数据类型"
Binding="{Binding SignalType}" /> Binding="{Binding CSharpDataType}" />
<DataGridTextColumn Header="S7地址" <DataGridTextColumn Header="S7地址"
Binding="{Binding S7Address}" /> Binding="{Binding S7Address}" />
</DataGrid.Columns> </DataGrid.Columns>