FolderPicker

파일 FolderPicker 시스템에서 폴더를 선택하는 기능을 제공합니다.

다음에 필요한 FolderPicker사전 조건은 다음과 같습니다.

에 사용 권한을 추가합니다.AndroidManifest.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

구문

C#

FolderPicker C#에서 다음과 같이 사용할 수 있습니다.

async Task PickFolder(CancellationToken cancellationToken)
{
    var result = await FolderPicker.Default.PickAsync(cancellationToken);
    if (result.IsSuccessful)
    {
        await Toast.Make($"The folder was picked: Name - {result.Folder.Name}, Path - {result.Folder.Path}", ToastDuration.Long).Show(cancellationToken);
    }
    else
    {
        await Toast.Make($"The folder was not picked with error: {result.Exception.Message}").Show(cancellationToken);
    }
}

폴더

레코드는 Folder 파일 시스템의 폴더를 나타냅니다. 이 파일은 다음 속성을 정의합니다.

  • 경로에 폴더 경로가 포함되어 있습니다.
  • 이름에 폴더 이름이 포함됩니다.

FolderPickerResult

에서 PickAsync정보를 저장합니다.

속성

속성 Type 설명
Folder Folder Folder 파일 시스템에서 선택한 폴더를 나타내는 폴더를 가져옵니다.
예외 Exception Exception 선택 작업이 실패한 경우를 가져옵니다.
IsSuccessful bool 작업이 성공했는지 여부를 결정하는 값을 가져옵니다.

메서드

메서드 설명
EnsureSuccess 선택 작업이 성공했는지 여부를 확인합니다.

Warning

EnsureSuccess 는 선택 작업이 실패한 경우 throw Exception 됩니다.

메서드

메서드 설명
PickAsync 사용 권한을 요청하고 파일 시스템에서 폴더를 선택할 수 있습니다.

종속성 등록

서비스를 삽입하려는 경우 먼저 등록해야 합니다. 다음 변경 내용으로 업데이트 MauiProgram.cs 합니다.

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .UseMauiCommunityToolkit();

        // Register the FolderPicker as a singleton
        builder.Services.AddSingleton<IFolderPicker>(FolderPicker.Default);
        
        // Register the MainPage as transient to make sure it can resolve the IFolderPicker dependency.
        builder.Services.AddTransient<MainPage>();
        return builder.Build();
    }
}

이제 다음과 같이 서비스를 삽입할 수 있습니다.

public partial class MainPage : ContentPage
{
    private readonly IFolderPicker folderPicker;

    public MainPage(IFolderPicker folderPicker)
    {
        InitializeComponent();
        this.folderPicker = folderPicker;
    }

    async Task PickFolder(CancellationToken cancellationToken)
    {
        var result = await folderPicker.PickAsync(cancellationToken);
        result.EnsureSuccess();
        await Toast.Make($"Folder picked: Name - {result.Folder.Name}, Path - {result.Folder.Path}", ToastDuration.Long).Show(cancellationToken);
    }
}

제공하는 CancellationToken 방법에 대한 자세한 내용은 Microsoft 설명서를 참조 하세요.

예제

.NET MAUI 커뮤니티 도구 키트 샘플 애플리케이션에서 작동 중인 FolderPicker 예제를 찾을 수 있습니다.

API

.NET MAUI 커뮤니티 도구 키트 GitHub 리포지토리에서 오버에 대한 FolderPicker 소스 코드를 찾을 수 있습니다.