728x90
728x170
■ Style<T> 클래스를 사용하는 방법을 보여준다.
▶ 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">
<Entry x:Name="entry"
HorizontalOptions="Center"
VerticalOptions="Center"
BackgroundColor="Yellow"
WidthRequest="100"
Placeholder="숫자 2자 이상 입력해 주시기 바랍니다." />
</ContentPage>
▶ MainPage.xaml.cs
using CommunityToolkit.Maui.Behaviors;
using CommunityToolkit.Maui.Markup;
namespace TestProject;
/// <summary>
/// 메인 페이지
/// </summary>
public partial class MainPage : ContentPage
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainPage()
/// <summary>
/// 생성자
/// </summary>
public MainPage()
{
InitializeComponent();
CharactersValidationBehavior behavior = new CharactersValidationBehavior
{
ValidStyle = new Style<Entry>(Entry.TextColorProperty, Colors.Green),
InvalidStyle = new Style<Entry>(Entry.TextColorProperty, Colors.Red),
Flags = ValidationFlags.ValidateOnValueChanged,
CharacterType = CharacterType.Digit,
MinimumCharacterTypeCount = 2
};
this.entry.Behaviors.Add(behavior);
}
#endregion
}
▶ 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
그리드형(광고전용)