public ImportViewModel() { ActiveVm = this; CancelCommand = new RelayCommand(OnCancel); SelectImportCommand = new RelayCommand<ImportModeViewModel>(OnSelectImport); CsvImportVm = new CsvImportViewModel(); WkImportVm = new WkImportViewModel(); _importModes = new ImportModeViewModel[] { CsvImportVm, WkImportVm }; foreach (ImportModeViewModel vm in _importModes) { vm.StepChanged += OnCurrentModeStepChanged; vm.Cancel += OnCurrentModeCanceled; vm.Finished += OnCurrentModeFinished; } }
public ImportViewModel() { ActiveVm = this; CancelCommand = new RelayCommand(OnCancel); SelectImportCommand = new RelayCommand <ImportModeViewModel>(OnSelectImport); CsvImportVm = new CsvImportViewModel(); WkImportVm = new WkImportViewModel(); _importModes = new ImportModeViewModel[] { CsvImportVm, WkImportVm }; foreach (ImportModeViewModel vm in _importModes) { vm.StepChanged += OnCurrentModeStepChanged; vm.Cancel += OnCurrentModeCanceled; vm.Finished += OnCurrentModeFinished; } }
public CsvImportColumnsStepViewModel(ImportModeViewModel parentMode) : base(parentMode) { _parent = (CsvImportViewModel)parentMode; NoTypeBehavior = CsvImportNoTypeBehavior.Auto; }
/// <summary> /// Attempts to load the file set in the file path as a CSV file and to send the resulting /// data to the parent mode view model. /// May be unsuccessful and return false. /// </summary> public bool TryLoadFile() { try { Encoding selectedEncoding; try { string encodingName = _selectedEncoding; if (string.IsNullOrWhiteSpace(encodingName)) { encodingName = "utf-8"; // Default to UTF-8 } selectedEncoding = Encoding.GetEncoding(encodingName); } catch (ArgumentException) { ErrorMessage = string.Format("The encoding name \"{0}\" is not supported.", _selectedEncoding); return(false); } using (CsvFileReader csvReader = new CsvFileReader(_filePath, EmptyLineBehavior.Ignore, selectedEncoding)) { // Set reading parameters. csvReader.Delimiter = _csvSeparator.First(); csvReader.Quote = _csvQuote.First(); CsvImportViewModel parent = (CsvImportViewModel)ParentMode; parent.CsvLines.Clear(); List <string> header = new List <string>(); List <string> row = new List <string>(); int maxColumns = 0; // Read header row if existing. if (_csvHasHeader) { csvReader.ReadRow(header); maxColumns = header.Count; } // Read data rows. while (csvReader.ReadRow(row)) { parent.CsvLines.Add(row); maxColumns = Math.Max(row.Count, maxColumns); row = new List <string>(); } // Add missing columns to the header, with generic names. // When there is no header, obviously, all columns are missing. while (header.Count < maxColumns) { header.Add(string.Format("[Column {0}]", header.Count + 1)); } parent.CsvColumns = header; } } catch (Exception ex) { // On exception, prevent going any further in the import process, display an error, and log it. ErrorMessage = string.Format("An unknown error occured while reading the CSV file:{0}{1}", Environment.NewLine, ex.Message); LogHelper.GetLogger("CSV Import").ErrorFormat("Error while reading CSV: {1}", ex.Message); return(false); } // No columns? if (!((CsvImportViewModel)ParentMode).CsvColumns.Any()) { ErrorMessage = "The file seems to be empty."; return(false); } // Aaand we're done. return(true); }