■ ListView 엘리먼트를 사용하는 방법을 보여준다.
▶ NamedColor.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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
using System.Reflection; namespace TestProject; /// <summary> /// 이름있는 색상 /// </summary> public class NamedColor { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 이름있는 색상 배열 /// </summary> private static NamedColor[] _namedColorArray; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 명칭 /// </summary> private string name; /// <summary> /// 색상 /// </summary> private Color color; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 이름있는 색상 배열 - NamedColorArray /// <summary> /// 이름있는 색상 배열 /// </summary> public static NamedColor[] NamedColorArray { get { return _namedColorArray; } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Public #region 명칭 - Name /// <summary> /// 명칭 /// </summary> public string Name { get { string name = this.name[0].ToString(); for(int i = 1; i < this.name.Length; i++) { name += (char.IsUpper(this.name[i]) ? " " : "") + this.name[i].ToString(); } return name; } } #endregion #region 색상 - Color /// <summary> /// 색상 /// </summary> public Color Color { get { return this.color; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Static #region 생성자 - NamedColor() /// <summary> /// 생성자 /// </summary> static NamedColor() { FieldInfo[] fieldInfoArray = typeof(Colors).GetFields(BindingFlags.Static | BindingFlags.Public); _namedColorArray = new NamedColor[fieldInfoArray.Length]; for(int i = 0; i < fieldInfoArray.Length; i++) { _namedColorArray[i] = new NamedColor ( fieldInfoArray[i].Name, (Color)fieldInfoArray[i].GetValue(null) ); } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Private #region 생성자 - NamedColor(name, color) /// <summary> /// 생성자 /// </summary> /// <param name="name">명칭</param> /// <param name="color">색상</param> private NamedColor(string name, Color color) { this.name = name; this.color = color; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 문자열 구하기 - ToString() /// <summary> /// 문자열 구하기 /// </summary> /// <returns>문자열</returns> public override string ToString() { return this.name; } #endregion } |
▶ ColorToStringConverter.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 |
using System.Globalization; namespace TestProject; /// <summary> /// 색상→문자열 변환자 /// </summary> public class ColorToStringConverter : IValueConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 변환하기 - (sourceValue, targetType, parameter, cultureInfo) /// <summary> /// 변환하기 /// </summary> /// <param name="sourceValue">소스 값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>변환 값</returns> public object Convert(object sourceValue, Type targetType, object parameter, CultureInfo cultureInfo) { Color color = sourceValue as Color; if(color == null) { return null; } int alpha = (int)Math.Round(255 * color.Alpha); int red = (int)Math.Round(255 * color.Red ); int green = (int)Math.Round(255 * color.Green); int blue = (int)Math.Round(255 * color.Blue ); return $"{alpha:X2}:{red:X2}:{green:X2}:{blue:X2}"; } #endregion #region 역변환하기 - ConvertBack(sourceValue, targetType, parameter, cultureInfo) /// <summary> /// 역변환하기 /// </summary> /// <param name="sourceValue">소스 값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>역변환 값</returns> /// <exception cref="NotImplementedException">미구현 예외</exception> public object ConvertBack(object sourceValue, Type targetType, object parameter, CultureInfo cultureInfo) { throw new NotImplementedException(); } #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 31 32 33 34 35 36 37 38 39 40 41 42 |
<?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> <x:Double x:Key="BoxSizeKey">50</x:Double> <x:Int32 x:Key="RowHeightKey">60</x:Int32> <local:ColorToStringConverter x:Key="ColorToStringConverterKey" /> </ContentPage.Resources> <ListView RowHeight="{StaticResource RowHeightKey}" ItemsSource="{x:Static local:NamedColor.NamedColorArray}"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <StackLayout Orientation="Horizontal" Padding="5,5,0,5" Spacing="15"> <BoxView WidthRequest="{StaticResource BoxSizeKey}" HeightRequest="{StaticResource BoxSizeKey}" Color="{Binding Color}" /> <StackLayout VerticalOptions="Center" Padding="5,0,0,0"> <Label FontSize="Medium" FontAttributes="Bold" Text="{Binding Name}" /> <Label Text="{Binding Color, Converter={StaticResource ColorToStringConverterKey}}" /> </StackLayout> </StackLayout> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> </ContentPage> |