LukeMauiFilePicker 1.2.0
dotnet add package LukeMauiFilePicker --version 1.2.0
NuGet\Install-Package LukeMauiFilePicker -Version 1.2.0
<PackageReference Include="LukeMauiFilePicker" Version="1.2.0" />
<PackageVersion Include="LukeMauiFilePicker" Version="1.2.0" />
<PackageReference Include="LukeMauiFilePicker" />
paket add LukeMauiFilePicker --version 1.2.0
#r "nuget: LukeMauiFilePicker, 1.2.0"
#:package LukeMauiFilePicker@1.2.0
#addin nuget:?package=LukeMauiFilePicker&version=1.2.0
#tool nuget:?package=LukeMauiFilePicker&version=1.2.0
This project is an attempt to temporarily fix dotnet/maui#11088 . This project also provides pickers experience across almost all platforms: WinUI, Android, iOS and Mac Catalyst. So far from my test, no permission is needed since you only have access to specific files that user picks. you only need to declare permission on MacOS, no other permission is needed on other platforms.
See demo project with sample text editor app: Opening a single file, Opening multiple files (concatenate all the content) and Saving into a file (location and name picked by user).
This article uses Windows screenshots. To see screenshots for other platforms, see Screenshots on all platforms
Was it helpful for you? Please consider a donation ❤️ PayPal.
Change Log
v1.2.0
- Fixed
SaveFileAsyncnot returning when user cancels the save operation on iOS and Mac Catalyst. - Added
DeferredSaveFileOptionsto allow deferring the content creation until after the user picks a file. Only work for Windows and Android. See documentation for more details.
Installation & Setup
Nuget Package
Install the NuGet package LukeMauiFilePicker into your .NET MAUI project:
dotnet add package LukeMauiFilePicker
Setup Dependency Injection
In your Program.cs file, add the following line to the CreateMauiApp() method:
// Add IFilePickerService service to your DI
builder.Services.AddFilePicker();
Note
If you use Android Save Picker, you need to add the code below to register Activity Result flow.
// You need this if you use Android Save Picker
// Optional Activity Result Request code, default is 2112 if not specified
builder.ConfigureFilePicker(100);
Warning
If you do not callConfigureFilePicker(), anInvalidOperationExceptionwill be thrown if you invoke Android Save Picker.
MacOS Permissions
On MacOS, you need to declare the following permissions in your Info.plist file. Choose one of the following options based on your needs:
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
Usage
The following methods can be called from an IFilePickerService instance from your DI.
Open File Picker (Pick Single and Multiple Files)
Task<IPickFile?> PickFileAsync(string title, Dictionary<DevicePlatform, IEnumerable<string>>? types);
Task<IEnumerable<IPickFile>?> PickFilesAsync(string title, Dictionary<DevicePlatform, IEnumerable<string>>? types, bool multiple);
Pick a single file or multiple files. The types parameter is a dictionary of platform-specific file types. If you do not specify the types parameter, all file types are allowed.
Windows Open Multiple Files Picker
Example:
static readonly Dictionary<DevicePlatform, IEnumerable<string>> FileType = new()
{
{ DevicePlatform.Android, new[] { "text/*" } } ,
{ DevicePlatform.iOS, new[] { "public.json", "public.plain-text" } },
{ DevicePlatform.MacCatalyst, new[] { "public.json", "public.plain-text" } },
{ DevicePlatform.WinUI, new[] { ".txt", ".json" } }
};
// Let user pick files (and handling cancelling)
var files = await picker.PickFilesAsync("Select a file", FileType, true);
if (files is null || !files.Any()) { return; }
// Read the files
foreach (var f in files)
{
using var s = await f.OpenReadAsync();
using var reader = new StreamReader(s);
str.AppendLine(await reader.ReadToEndAsync());
}
Note
On some platforms that official MAUI File picker works, the library simply calls those APIs.
The IPickFile interface lets you know the FileName and openning a Stream to a file. Optionally, you can also get the original FileResult from MAUI API if this library doesn't have custom implementation for it.
Warning
On some platforms, when user cancel (pressing Back button),PickFilesAsyncreturns an empty array instead ofnullso you need to check forAny()as well.
Note
For iOS and Mac Catalyst, you can put in eitherIdentifier(public.json),MimeType(application/json) or fileExtension(json). The library attempt to call 3 create methodsCreateFromIdentifier,CreateFromMimeType,CreateFromExtensionone after another. If all 3 methods fail,UTTypes.Itemis used.
Save File Picker
Task<bool> SaveFileAsync(SaveFileOptions options);
Task<bool> SaveFileAsync(DeferredSaveFileOptions options);
Save a file. The SaveFileOptions and DeferredSaveFileOptions contains the following properties:
string SuggestedFileName(Required): The suggested file name.Stream ContentforSaveFileOptions(Required): The content to write to. Note that this is required before user even pick a file because for iOS and Mac, it needs to be available before user picks a file.Func<Stream>orFunc<Task<Stream>> ContentforDeferredSaveFileOptions: A function that returns a stream to write to. This is useful when you want to defer the content creation until after the user picks a file. The function is not called if user cancels the save operation. Note: on iOS and Mac Catalyst, this function simply redirects toSaveFileOptions.Contentsince the content needs to be available before user picks a file.(string FileTypeName, List<string> FileTypeExts) WindowsFileTypes: Windows-specific file types. TheFileTypeNameis the name of the file type (can be any descriptive text), andFileTypeExtsis a list of file extensions. If you do not specify this, the default isAll Files (*.*).string AndroidMimeType: Android-specific MIME type. If you do not specify this, the default isapplication/octet-stream.
The method returns bool to indicate if the user successfully picked a file and saved the content.
NOTE
There is no specific iOS and Mac Catalyst option.
DeferredSaveFileOptionsacts just likeSaveFileOptionson iOS and Mac Catalyst since the content needs to be available before user picks a file.
Example:
var bytes = Encoding.UTF8.GetBytes(TextEditor.Text ?? "");
using var memory = new MemoryStream(bytes);
await picker.SaveFileAsync(new("text.txt", memory)
{
AndroidMimeType = "text/plain",
WindowsFileTypes = ("Text files", new() { ".txt", })
});
Warning
As specified on Setup Dependency Injection, you need to callConfigureFilePicker()to register Activity Result flow if you use Android Save Picker.
Screenshots on all platforms
See the imgs folder.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0-android34.0 is compatible. net8.0-ios18.0 is compatible. net8.0-maccatalyst18.0 is compatible. net8.0-windows10.0.19041 is compatible. net9.0-android was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-windows was computed. net10.0-android was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-windows was computed. |
-
net8.0-android34.0
- No dependencies.
-
net8.0-ios18.0
- No dependencies.
-
net8.0-maccatalyst18.0
- No dependencies.
-
net8.0-windows10.0.19041
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.