C#/Xamarin Maui
[.NET MAUI] Share
kjun.kr
2023. 4. 12. 12:16
728x90
728x170
앱에서 text,uri,file 을 Share 하는 방법입니다.
iOS 에는 아래의 내용이 info.list 에 추가되어야합니다.
<key>NSPhotoLibraryAddUsageDescription</key> <string>This app needs access to the photo gallery to save photos and videos.</string> <key>NSPhotoLibraryUsageDescription</key> <string>This app needs access to the photo gallery to save photos and videos.</string> |
MainPage.xaml
<HorizontalStackLayout>
<Entry x:Name="shareEntry" Text="http://kjun.kr" />
<Button
x:Name="textShareButton"
Margin="5"
Clicked="textShareButton_Clicked"
Text="Test Share" />
<Button
x:Name="uriShareButton"
Margin="5"
Clicked="uriShareButton_Clicked"
Text="Uri Share" />
</HorizontalStackLayout>
MainPage.xaml.cs
private async void textShareButton_Clicked(object sender, EventArgs e)
{
await ShareText(this.shareEntry.Text);
}
private async void uriShareButton_Clicked(object sender, EventArgs e)
{
await ShareUri(this.shareEntry.Text, Share.Default);
}
public async Task ShareText(string text)
{
if (string.IsNullOrEmpty(text)) return;
await Share.Default.RequestAsync(new ShareTextRequest
{
Text = text,
Title = "Share Text"
});
}
public async Task ShareUri(string uri, IShare share)
{
if (string.IsNullOrEmpty(uri)) return;
await share.RequestAsync(new ShareTextRequest
{
Uri = uri,
Title = "Share Web Link"
});
}
public async Task ShareFile()
{
string fn = "Attachment.txt";
string file = Path.Combine(FileSystem.CacheDirectory, fn);
File.WriteAllText(file, "Hello World");
await Share.Default.RequestAsync(new ShareFileRequest
{
Title = "Share text file",
File = new ShareFile(file)
});
}
결과
[Source]
https://github.com/kei-soft/Maui.BasicApp
728x90
그리드형