using System.Collections; using System.Collections.ObjectModel; using System.Linq; using AutoMapper; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using DMS.Core.Interfaces.Services; using DMS.Core.Models; using DMS.WPF.Interfaces; using DMS.WPF.Services; using DMS.WPF.ItemViewModel; namespace DMS.WPF.ViewModels.Dialogs; public partial class ImportExcelDialogViewModel : DialogViewModelBase> { private readonly IMapper _mapper; private readonly IExcelService _excelService; private readonly INotificationService _notificationService; [ObservableProperty] private string? _filePath; [ObservableProperty] private List _variables = new(); [ObservableProperty] private ObservableCollection _variableItemViewModels ; [ObservableProperty] private IList _selectedVariables = new ArrayList(); public ImportExcelDialogViewModel(IMapper mapper,IExcelService excelService, INotificationService notificationService) { _mapper = mapper; _excelService = excelService; _notificationService = notificationService; VariableItemViewModels = new(); } partial void OnFilePathChanged(string? value) { if (string.IsNullOrEmpty(value)) { return; } try { Variables = _excelService.ImprotFromTiaVariableTable(value); VariableItemViewModels=new ObservableCollection(_mapper.Map>(Variables)); } catch (System.Exception ex) { _notificationService.ShowError($"从Excel文件中读取变量时发生了错误:{ex.Message}",ex); } } [RelayCommand] private void ImportAll() { Close(Variables); } [RelayCommand] private void ImportSelected() { var selected = SelectedVariables.Cast().ToList(); Close(_mapper.Map>(selected)); } [RelayCommand] private void CancleButton() { Close(null); } }