728x90
반응형
728x170
▶ MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="TestProject.MainPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<StackLayout
HorizontalOptions="Center"
VerticalOptions="Center"
Spacing="10">
<Button x:Name="createTestFileButton"
HorizontalOptions="Center"
Text="테스트 파일 생성하기" />
<Button x:Name="shareFileButton"
HorizontalOptions="Center"
Text="파일 공유하기" />
</StackLayout>
</ContentPage>
728x90
▶ MainPage.xaml.cs
namespace TestProject;
/// <summary>
/// 메인 페이지
/// </summary>
public partial class MainPage : ContentPage
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 파일명
/// </summary>
private string fileName = "sample.txt";
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainPage()
/// <summary>
/// 생성자
/// </summary>
public MainPage()
{
InitializeComponent();
this.createTestFileButton.Clicked += createTestFileButton_Clicked;
this.shareFileButton.Clicked += shareFileButton_Clicked;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 테스트 파일 생성하기 버튼 클릭시 처리하기 - createTestFileButton_Clicked(sender, e)
/// <summary>
/// 테스트 파일 생성하기 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void createTestFileButton_Clicked(object sender, EventArgs e)
{
string filePath = Path.Combine(FileSystem.CacheDirectory, this.fileName);
File.WriteAllText(filePath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
#endregion
#region 파일 공유하기 버튼 클릭시 처리하기 - shareFileButton_Clicked(sender, e)
/// <summary>
/// 파일 공유하기 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private async void shareFileButton_Clicked(object sender, EventArgs e)
{
string filePath = Path.Combine(FileSystem.CacheDirectory, this.fileName);
if(!File.Exists(filePath))
{
await DisplayAlert("INFORMATION", "테스트 파일을 생성해 주시기 바랍니다.", "확인");
return;
}
await Share.Default.RequestAsync
(
new ShareFileRequest
{
Title = "텍스트 파일 공유",
File = new ShareFile(filePath)
}
);
}
#endregion
}
728x90
반응형
그리드형(광고전용)
댓글을 달아 주세요