■ EnumToBoolConverter 엘리먼트에서 열거형 값 → 진리 값 변환자를 사용하는 방법을 보여준다.
▶ OSTYpe.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 28 29 30 31 32 33 34 |
namespace TestProject; /// <summary> /// OS 타입 /// </summary> public enum OSType { /// <summary> /// 안드로이드 /// </summary> Android, /// <summary> /// iOS /// </summary> iOS, /// <summary> /// 맥 OS /// </summary> macOS, /// <summary> /// 타이젠 /// </summary> Tizen, /// <summary> /// 윈도우즈 /// </summary> Windows } |
▶ MainPage.xaml
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 28 29 30 31 32 33 34 35 36 37 |
<?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:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit" xmlns:local="clr-namespace:TestProject"> <ContentPage.Resources> <toolkit:EnumToBoolConverter x:Key="EnumToBoolConverterKey1"> <toolkit:EnumToBoolConverter.TrueValues> <local:OSType>Android</local:OSType> <local:OSType>iOS</local:OSType> </toolkit:EnumToBoolConverter.TrueValues> </toolkit:EnumToBoolConverter> <toolkit:EnumToBoolConverter x:Key="EnumToBoolConverterKey2"> <toolkit:EnumToBoolConverter.TrueValues> <local:OSType>Tizen</local:OSType> <local:OSType>Windows</local:OSType> </toolkit:EnumToBoolConverter.TrueValues> </toolkit:EnumToBoolConverter> </ContentPage.Resources> <StackLayout HorizontalOptions="Center" VerticalOptions="Center"> <Label HorizontalOptions="Center" FontSize="20" Text="안드로이드 또는 iOS 운영체제 입니다." IsVisible="{Binding SelectedValue, Converter={StaticResource EnumToBoolConverterKey1}}" /> <Label HorizontalOptions="Center" FontSize="20" Text="타이젠 또는 윈도우즈 운영체제 입니다." IsVisible="{Binding SelectedValue, Converter={StaticResource EnumToBoolConverterKey2}}" /> </StackLayout> </ContentPage> |
※ EnumToBoolConverter.TrueValues 속성에서 열거형 값이 1개인 경우는 컴파일시 에러가 발생했으며 이 경우는 해당 열거형 값을 한번 더 서술하였다.
▶ MainPage.xaml.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 28 29 30 31 32 33 34 35 36 37 38 39 40 |
namespace TestProject; /// <summary> /// 메인 페이지 /// </summary> public partial class MainPage : ContentPage { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 선택 값 - SelectedValue /// <summary> /// 선택 값 /// </summary> public OSType SelectedValue { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); SelectedValue = OSType.iOS; BindingContext = this; } #endregion } |
▶ MauiProgram.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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
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 } |