■ EventTrigger 엘리먼트를 사용하는 방법을 보여준다.
▶ ValidateEntryTextChangedAction.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
namespace TestProject; /// <summary> /// 엔트리 텍스트 변경시 검증 액션 /// </summary> public class ValidateEntryTextChangedAction : TriggerAction<Entry> { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 호출하기 - Invoke(entry) /// <summary> /// 호출하기 /// </summary> /// <param name="entry">엔트리</param> protected override void Invoke(Entry entry) { bool isValid = Double.TryParse(entry.Text, out double result); entry.TextColor = isValid ? Colors.Black : Colors.Red; } #endregion } |
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?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" xmlns:local="clr-namespace:TestProject"> <StackLayout HorizontalOptions="Center" VerticalOptions="Center"> <Entry x:Name="entry" WidthRequest="300" Text="" Placeholder="텍스트를 입력해주시기 바랍니다."> <Entry.Triggers> <EventTrigger Event="TextChanged"> <local:ValidateEntryTextChangedAction /> </EventTrigger> </Entry.Triggers> </Entry> </StackLayout> </ContentPage> |