diff --git a/XpathRunner/Service/DialogService.cs b/XpathRunner/Service/DialogService.cs index a8b0438..d9bc7ae 100644 --- a/XpathRunner/Service/DialogService.cs +++ b/XpathRunner/Service/DialogService.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Linq; using System.Threading.Tasks; using Avalonia; @@ -29,6 +30,37 @@ public class DialogService return fileList; } + public async Task SaveResultsToCsvAsync(string[] content) + { + var result = string.Join(Environment.NewLine, content); + await ExportFile(result); + } + + private async Task ExportFile(string content) + { + if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime + { + MainWindow.StorageProvider: { } provider + }) return; + + var options = new FilePickerSaveOptions + { + Title = "Save File", + SuggestedFileName = "NewFile.txt", + DefaultExtension = "txt", + ShowOverwritePrompt = true + }; + + var file = await provider.SaveFilePickerAsync(options); + + if (file != null) + { + await using var stream = await file.OpenWriteAsync(); + await using var writer = new StreamWriter(stream); + await writer.WriteAsync(content); + } + } + private static FilePickerFileType[] AllowedFileTypes() { return new[] @@ -39,4 +71,6 @@ private static FilePickerFileType[] AllowedFileTypes() } }; } + + } \ No newline at end of file diff --git a/XpathRunner/ViewModels/MainWindowViewModel.cs b/XpathRunner/ViewModels/MainWindowViewModel.cs index 2d00f1b..e95f37f 100644 --- a/XpathRunner/ViewModels/MainWindowViewModel.cs +++ b/XpathRunner/ViewModels/MainWindowViewModel.cs @@ -69,6 +69,14 @@ public MainWindowViewModel() UpdateSelectedFilesLabel(); } }; + + ExportResultsCommand = new RelayCommand(async () => + { + IsBusy = true; + var content = XpathResults.ToArray(); + await _dialogService.SaveResultsToCsvAsync(content); + IsBusy = false; + }); } #region Public properties @@ -139,6 +147,7 @@ public string SelectedFilesLabel public ICommand GetXpathResultsCommand { get; } public ICommand AddFilesFileCommand { get; } public ICommand RemoveFileCommand { get; } + public ICommand ExportResultsCommand { get; } public ObservableCollection? SelectedFiles { diff --git a/XpathRunner/Views/MainView.axaml b/XpathRunner/Views/MainView.axaml index 61e5acc..0735ca1 100644 --- a/XpathRunner/Views/MainView.axaml +++ b/XpathRunner/Views/MainView.axaml @@ -70,6 +70,7 @@ +