public WindowScreenShotControl() { InitializeComponent(); WindowScreenShotCommand = new Command <Window>(async x => { if (x == null) { return; } StatusControlContext statusContext = null; try { statusContext = (StatusControlContext)((dynamic)x.DataContext).StatusContext; } catch (Exception e) { Console.WriteLine(e); } var result = await FrameworkElementScreenShot.TryScreenShotToClipboardAsync(x); if (statusContext != null) { if (result) { statusContext.ToastSuccess("Window copied to Clipboard"); } else { statusContext.ToastError("Problem Copying Window to Clipboard"); } } }); DataContext = this; }
public static async Task RenameSelectedFile(FileInfo selectedFile, StatusControlContext statusContext, Action <FileInfo> setSelectedFile) { if (selectedFile == null || !selectedFile.Exists) { statusContext.ToastWarning("No file to rename?"); return; } var newName = await statusContext.ShowStringEntry("Rename File", $"Rename {Path.GetFileNameWithoutExtension(selectedFile.Name)} - " + "File Names must be limited to A-Z a-z 0-9 - . _ :", Path.GetFileNameWithoutExtension(selectedFile.Name)); if (!newName.Item1) { return; } var cleanedName = newName.Item2.TrimNullToEmpty(); if (string.IsNullOrWhiteSpace(cleanedName)) { statusContext.ToastError("Can't rename the file to an empty string..."); return; } var noExtensionCleaned = Path.GetFileNameWithoutExtension(cleanedName); if (string.IsNullOrWhiteSpace(noExtensionCleaned)) { statusContext.ToastError("Not a valid filename..."); return; } if (!FolderFileUtility.IsNoUrlEncodingNeeded(noExtensionCleaned)) { statusContext.ToastError("File Names must be limited to A - Z a - z 0 - 9 - . _"); return; } var moveToName = Path.Combine(selectedFile.Directory?.FullName ?? string.Empty, $"{noExtensionCleaned}{Path.GetExtension(selectedFile.Name)}"); try { File.Copy(selectedFile.FullName, moveToName); } catch (Exception e) { await EventLogContext.TryWriteExceptionToLog(e, statusContext.StatusControlContextId.ToString(), "Exception while trying to rename file."); statusContext.ToastError($"Error Copying File: {e.Message}"); return; } var finalFile = new FileInfo(moveToName); if (!finalFile.Exists) { statusContext.ToastError("Unknown error renaming file - original file still selected."); return; } try { setSelectedFile(finalFile); } catch (Exception e) { statusContext.ToastError($"Error setting selected file - {e.Message}"); return; } statusContext.ToastSuccess($"Selected file now {selectedFile.FullName}"); }
public static async Task ImportFromExcel(StatusControlContext statusContext) { await ThreadSwitcher.ThreadSwitcher.ResumeBackgroundAsync(); statusContext.Progress("Starting excel load."); var dialog = new VistaOpenFileDialog(); if (!(dialog.ShowDialog() ?? false)) { return; } var newFile = new FileInfo(dialog.FileName); if (!newFile.Exists) { statusContext.ToastError("File doesn't exist?"); return; } ExcelContentImports.ExcelContentTableImportResults contentTableImportResult; try { contentTableImportResult = await ExcelContentImports.ImportFromFile(newFile.FullName, statusContext.ProgressTracker()); } catch (Exception e) { await statusContext.ShowMessageWithOkButton("Import File Errors", $"Import Stopped because of an error processing the file:{Environment.NewLine}{e.Message}"); return; } if (contentTableImportResult.HasError) { await statusContext.ShowMessageWithOkButton("Import Errors", $"Import Stopped because errors were reported:{Environment.NewLine}{contentTableImportResult.ErrorNotes}"); return; } var shouldContinue = await statusContext.ShowMessage("Confirm Import", $"Continue?{Environment.NewLine}{Environment.NewLine}{contentTableImportResult.ToUpdate.Count} updates from {newFile.FullName} {Environment.NewLine}" + $"{string.Join(Environment.NewLine, contentTableImportResult.ToUpdate.Select(x => $"{Environment.NewLine}{x.Title}{Environment.NewLine}{x.DifferenceNotes}"))}", new List <string> { "Yes", "No" }); if (shouldContinue == "No") { return; } var saveResult = await ExcelContentImports.SaveAndGenerateHtmlFromExcelImport(contentTableImportResult, statusContext.ProgressTracker()); if (saveResult.hasError) { await statusContext.ShowMessageWithOkButton("Excel Import Save Errors", $"There were error saving changes from the Excel Content:{Environment.NewLine}{saveResult.errorMessage}"); return; } statusContext.ToastSuccess( $"Imported {contentTableImportResult.ToUpdate.Count} items with changes from {newFile.FullName}"); }