-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathFolderPickerImplementation.windows.cs
More file actions
53 lines (44 loc) · 1.59 KB
/
FolderPickerImplementation.windows.cs
File metadata and controls
53 lines (44 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using CommunityToolkit.Maui.Core.Primitives;
using Microsoft.Maui.ApplicationModel;
using Microsoft.UI;
using Microsoft.Windows.AppLifecycle;
using Microsoft.Windows.Storage.Pickers;
namespace CommunityToolkit.Maui.Storage;
/// <inheritdoc />
public sealed partial class FolderPickerImplementation : IFolderPicker
{
async Task<Folder> InternalPickAsync(string initialPath, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (IPlatformApplication.Current?.Application.Windows[0].Handler?.PlatformView is not MauiWinUIWindow window)
{
throw new FolderPickerException(
"Cannot present folder picker: No active window found. Ensure the app is active with a visible window.");
}
var folderPicker = new Microsoft.Windows.Storage.Pickers.FolderPicker(window.AppWindow.Id)
{
SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
SuggestedFolder = initialPath
};
var folderPickerOperation = folderPicker.PickSingleFolderAsync();
void CancelFolderPickerOperation()
{
folderPickerOperation.Cancel();
}
await using var _ = cancellationToken.Register(CancelFolderPickerOperation);
var folder = await folderPickerOperation;
if (folder is null)
{
throw new OperationCanceledException("Operation cancelled.");
}
if (string.IsNullOrEmpty(folder.Path))
{
throw new FolderPickerException("Folder doesn't exist.");
}
return new Folder(folder.Path, new DirectoryInfo(folder.Path).Name);
}
Task<Folder> InternalPickAsync(CancellationToken cancellationToken)
{
return InternalPickAsync(string.Empty, cancellationToken);
}
}