파일 저장을 위한 FileSaver 및 폴더 선택을 위한 FolderPicker에 대해 자세히 알아보십시오.
iOS는 정상적으로 동작하나 파일명은 GUI 형태로 저장됩니다.
(파일명은 text.txt로 지정했는데 guid.txt로 저장됩니다.
)Android에서는 일반적으로 Android12 버전에서만 작동합니다.
Android13 버전에서는 메모리 권한을 얻지 못해서(문제로 보고됨) 정상적으로 동작하지 않습니다.
Android12 시뮬레이터를 사용하여 하나를 만들고 테스트했습니다.
MauiProgram.cs
using CommunityToolkit.Maui;
using CommunityToolkit.Maui.Storage;
namespace Maui.ToolKitMaui
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseMauiCommunityToolkit()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
// FileSaver 주입
builder.Services.AddSingleton<IFileSaver>(FileSaver.Default);
builder.Services.AddTransient<MainPage>();
return builder.Build();
}
}
}
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="Maui.ToolKitMaui.MainPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:animations="clr-namespace:Maui.ToolKitMaui.Animations"
xmlns:mct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
BackgroundColor="White">
<ScrollView>
<VerticalStackLayout
Padding="30,0"
Spacing="15"
VerticalOptions="Center">
<Entry
x:Name="saveEntry"
BackgroundColor="LightGray"
Text="Save Content"
TextColor="Black" />
<HorizontalStackLayout>
<Button
x:Name="fileButton"
Clicked="fileButton_Clicked"
HorizontalOptions="Center"
Text="Save File" />
<Label
x:Name="savePathLabel"
Margin="10,0"
TextColor="Black"
VerticalTextAlignment="Center" />
</HorizontalStackLayout>
<HorizontalStackLayout>
<Button
x:Name="pickButton"
Clicked="pickButton_Clicked"
HorizontalOptions="Center"
Text="Pick Folder" />
<Label
x:Name="pickPathLabel"
Margin="10,0"
TextColor="Black"
VerticalTextAlignment="Center" />
</HorizontalStackLayout>
</VerticalStackLayout>
</ScrollView>
</ContentPage>
MainPage.xaml.cs
using System.Text;
using CommunityToolkit.Maui.Alerts;
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Storage;
using Font = Microsoft.Maui.Font;
namespace Maui.ToolKitMaui
{
public partial class MainPage : ContentPage
{
IFileSaver fileSaver;
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
public MainPage(IFileSaver fileSaver)
{
InitializeComponent();
this.fileSaver = fileSaver;
this.BindingContext = new MainViewModel();
CheckPermission();
}
private async void fileButton_Clicked(object sender, EventArgs e)
{
using var stream = new MemoryStream(Encoding.Default.GetBytes(this.saveEntry.Text));
var result = await this.fileSaver.SaveAsync("test.txt", stream, cancellationTokenSource.Token);
if (result.IsSuccessful)
{
this.savePathLabel.Text = result.FilePath;
}
}
private async void pickButton_Clicked(object sender, EventArgs e)
{
var result = await FolderPicker.PickAsync(cancellationTokenSource.Token);
if (result.IsSuccessful)
{
this.pickPathLabel.Text = result.Folder.Path;
}
}
private async void CheckPermission()
{
PermissionStatus readStatus = await Permissions.CheckStatusAsync<Permissions.StorageRead>();
if (readStatus !
= PermissionStatus.Granted)
{
if (Permissions.ShouldShowRationale<Permissions.StorageRead>())
{
await DisplayAlert("Permission", "Need Permissions", "OK");
}
var status = await Permissions.RequestAsync<Permissions.StorageRead>();
if (status !
= PermissionStatus.Granted)
{
await DisplayAlert("Permission Check", "Need Permissions", "OK");
return;
}
if (readStatus == PermissionStatus.Granted)
{
PermissionStatus writeStatus = await Permissions.CheckStatusAsync<Permissions.StorageWrite>();
if (writeStatus !
= PermissionStatus.Granted)
{
if (Permissions.ShouldShowRationale<Permissions.StorageWrite>())
{
await DisplayAlert("Permission", "Need Permissions", "OK");
}
status = await Permissions.RequestAsync<Permissions.StorageWrite>();
if (status !
= PermissionStatus.Granted)
{
await DisplayAlert("Permission Check", "Need Permissions", "OK");
return;
}
}
}
}
}
}
}
안드로이드 결과
(파일 저장시 test폴더에 test.txt 파일이 있어서 덮어쓰기 했습니다.
)
iOS 결과
(아래 이미지와 같이 파일 저장시 파일명을 text.txt로 지정했는데도 guid.txt로 저장합니다.
이거 오류인가요;;)
(원천)
https://github.com/kei-soft/KJunBlog/tree/master/Maui.ToolKitMaui
참조
https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/essentials/