[C#/MAUI/.NET6] StateToBooleanConverter 클래스 : LayoutState → 진리 값 변환자 사용하기
C#/MAUI 2022. 7. 25. 23:25728x90
반응형
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">
<VerticalStackLayout
HorizontalOptions="Center"
VerticalOptions="Center"
Spacing="10">
<Label x:Name="label"
HorizontalOptions="Center" />
<Button x:Name="changeButton"
Text="상태 변경하기" />
</VerticalStackLayout>
</ContentPage>
300x250
▶ MainPage.xaml.cs
using CommunityToolkit.Maui.Converters;
namespace TestProject;
/// <summary>
/// 메인 페이지
/// </summary>
public partial class MainPage : ContentPage
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Bindable Property
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region 레이아웃 상태 속성 - LayoutStateProperty
/// <summary>
/// 레이아웃 상태 속성
/// </summary>
public static readonly BindableProperty LayoutStateProperty = BindableProperty.Create
(
"LayoutState",
typeof(LayoutState),
typeof(MainPage),
LayoutState.None
);
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 레이아웃 상태 - LayoutState
/// <summary>
/// 레이아웃 상태
/// </summary>
public LayoutState LayoutState
{
get => (LayoutState)GetValue(LayoutStateProperty);
set => SetValue(LayoutStateProperty, value);
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainPage()
/// <summary>
/// 생성자
/// </summary>
public MainPage()
{
InitializeComponent();
StateToBooleanConverter converter = new StateToBooleanConverter();
converter.StateToCompare = LayoutState.Success;
this.label.SetBinding
(
Label.TextProperty,
new Binding("LayoutState", source : this, converter : converter)
);
this.changeButton.Clicked += changeButton_Clicked;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 상태 변경하기 버튼 클릭시 처리하기 - changeButton_Clicked(sender, e)
/// <summary>
/// 상태 변경하기 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void changeButton_Clicked(object sender, EventArgs e)
{
LayoutState = LayoutState switch
{
LayoutState.None => LayoutState.Success,
_ => LayoutState.None
};
}
#endregion
}
728x90
▶ MauiProgram.cs
using CommunityToolkit.Maui;
namespace TestProject;
/// <summary>
/// MAUI 프로그램
/// </summary>
public static class MauiProgram
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region MAUI 앱 생성하기 - CreateMauiApp()
/// <summary>
/// MAUI 앱 생성하기
/// </summary>
/// <returns>MAUI 앱</returns>
public static MauiApp CreateMauiApp()
{
MauiAppBuilder builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseMauiCommunityToolkit()
.ConfigureFonts
(
fontCollection =>
{
fontCollection.AddFont("OpenSans-Regular.ttf" , "OpenSansRegular" );
fontCollection.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
}
);
return builder.Build();
}
#endregion
}
728x90
반응형
그리드형(광고전용)
댓글을 달아 주세요