■ StackLayout 엘리먼트에서 BindableLayout.ItemsSource 첨부 속성을 사용하는 방법을 보여준다.
▶ FontModel.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 |
namespace TestProject; /// <summary> /// 폰트 모델 /// </summary> public class FontModel { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 아이콘 - Icon /// <summary> /// 아이콘 /// </summary> public ImageSource Icon { get; set; } #endregion #region 글리프 - Glyph /// <summary> /// 글리프 /// </summary> public string Glyph { get; set; } #endregion } |
▶ FontCollection.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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
using System.Globalization; using System.Text.RegularExpressions; namespace TestProject { /// <summary> /// 폰트 컬렉션 /// </summary> public class FontCollection : List<FontModel> { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - FontCollection() /// <summary> /// 생성자 /// </summary> public FontCollection() { for(int i = 0xf0ff ; i < 0xf3aa; i++) { string escapedUnicode = "\\u" + i.ToString("x4"); string nonEscapedUnicode = GetNonEscapedUnicodeString(escapedUnicode); FontImageSource fontImageSource = new FontImageSource(); fontImageSource.Color = Colors.Blue; fontImageSource.FontFamily = "ionicons"; fontImageSource.Glyph = nonEscapedUnicode; Add(new FontModel { Icon = fontImageSource, Glyph = escapedUnicode }); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 비 이스케이프 유니코드 문자열 구하기 - GetNonEscapedUnicodeString(sourceEscapedUnicodeString) /// <summary> /// 비 이스케이프 유니코드 문자열 구하기 /// </summary> /// <param name="sourceEscapedUnicodeString">소스 이스케이프 유니코드 문자열</param> /// <returns>비 이스케이프 유니코드 문자열</returns> private string GetNonEscapedUnicodeString(string sourceEscapedUnicodeString) { return Regex.Replace ( sourceEscapedUnicodeString, @"\\u(?<Value>[a-zA-Z0-9]{4})", m => { return ((char)int.Parse(m.Groups["Value"].Value, NumberStyles.HexNumber)).ToString(); } ); } #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 23 24 25 26 27 28 29 30 |
<?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"> <ContentPage.Resources> <local:FontCollection x:Key="FontCollectionKey" /> </ContentPage.Resources> <ScrollView Margin="20"> <StackLayout BindableLayout.ItemsSource="{StaticResource FontCollectionKey}"> <BindableLayout.ItemTemplate> <DataTemplate> <StackLayout Orientation="Horizontal"> <Image VerticalOptions="Center" WidthRequest="32" HeightRequest="32" Source="{Binding Icon}" /> <Label Margin="10,0,0,0" VerticalOptions="Center" Text="{Binding Glyph}" /> </StackLayout> </DataTemplate> </BindableLayout.ItemTemplate> </StackLayout> </ScrollView> </ContentPage> |