From 933d11142e452c23844ebbde547aee6a11ff93bf Mon Sep 17 00:00:00 2001 From: "Matthew Parker [SSW]" <61717342+MattParkerDev@users.noreply.github.com> Date: Sun, 21 Jan 2024 14:21:56 +1000 Subject: [PATCH] File filters --- .../Services/FileService.cs | 25 ++++++++++++++++--- .../ViewModels/MainWindowViewModel.cs | 4 +-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/DotNetSolutionTools.App/Services/FileService.cs b/DotNetSolutionTools.App/Services/FileService.cs index 72441f0..409f6a7 100644 --- a/DotNetSolutionTools.App/Services/FileService.cs +++ b/DotNetSolutionTools.App/Services/FileService.cs @@ -6,7 +6,26 @@ namespace DotNetSolutionTools.App.Services; public class FileService { - public async Task DoOpenFilePickerAsync() + private readonly FilePickerFileType _csprojFileType = new("C# Project File") + { + Patterns = new[] { "*.csproj" } + }; + private readonly FilePickerFileType _slnFileType = new("C# Solution File") + { + Patterns = new[] { "*.sln" } + }; + + public async Task DoOpenFilePickerCsprojAsync() + { + return await DoOpenFilePickerAsync(_csprojFileType); + } + + public async Task DoOpenFilePickerSlnAsync() + { + return await DoOpenFilePickerAsync(_slnFileType); + } + + public async Task DoOpenFilePickerAsync(FilePickerFileType fileType) { if ( Application.Current?.ApplicationLifetime @@ -16,7 +35,7 @@ public class FileService throw new NullReferenceException("Missing StorageProvider instance."); var files = await provider.OpenFilePickerAsync( - new FilePickerOpenOptions() { Title = "Open Text File", AllowMultiple = false } + new FilePickerOpenOptions() { Title = "Open File", AllowMultiple = false, FileTypeFilter = [fileType] } ); return files?.Count >= 1 ? files[0] : null; @@ -32,7 +51,7 @@ public class FileService throw new NullReferenceException("Missing StorageProvider instance."); var folder = await provider.OpenFolderPickerAsync( - new FolderPickerOpenOptions() { Title = "Open Text File", AllowMultiple = false } + new FolderPickerOpenOptions() { Title = "Select Folder", AllowMultiple = false } ); return folder?.Count >= 1 ? folder[0] : null; diff --git a/DotNetSolutionTools.App/ViewModels/MainWindowViewModel.cs b/DotNetSolutionTools.App/ViewModels/MainWindowViewModel.cs index cbde896..37ed810 100644 --- a/DotNetSolutionTools.App/ViewModels/MainWindowViewModel.cs +++ b/DotNetSolutionTools.App/ViewModels/MainWindowViewModel.cs @@ -243,7 +243,7 @@ public partial class MainWindowViewModel : ViewModelBase { try { - var file = await _fileService.DoOpenFilePickerAsync(); + var file = await _fileService.DoOpenFilePickerSlnAsync(); if (file is null) return; @@ -295,7 +295,7 @@ public partial class MainWindowViewModel : ViewModelBase { try { - var folder = await _fileService.DoOpenFilePickerAsync(); + var folder = await _fileService.DoOpenFilePickerCsprojAsync(); if (folder is null) return;