■ IMarkupExtension<T> 인터페이스에서 커스텀 마크업 확장을 사용하는 방법을 보여준다.
▶ HSLColorExtension.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 |
namespace TestProject; /// <summary> /// HSL 색상 확장 /// </summary> public class HSLColorExtension : IMarkupExtension<Color> { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region H - H /// <summary> /// H /// </summary> public float H { get; set; } #endregion #region S - S /// <summary> /// S /// </summary> public float S { get; set; } #endregion #region L - L /// <summary> /// L /// </summary> public float L { get; set; } #endregion #region A - A /// <summary> /// A /// </summary> public float A { get; set; } = 1.0f; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 값 제공하기 - ProvideValue(serviceProvider) /// <summary> /// 값 제공하기 /// </summary> /// <param name="serviceProvider">서비스 제공자</param> /// <returns>값</returns> public Color ProvideValue(IServiceProvider serviceProvider) { return Color.FromHsla(H, S, L, A); } #endregion #region 값 제공하기 - IMarkupExtension.ProvideValue(serviceProvider) /// <summary> /// 값 제공하기 /// </summary> /// <param name="serviceProvider">서비스 제공자</param> /// <returns>값</returns> object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider) { return (this as IMarkupExtension<Color>).ProvideValue(serviceProvider); } #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 |
<?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> <Style TargetType="BoxView"> <Setter Property="HorizontalOptions" Value="Center" /> <Setter Property="VerticalOptions" Value="Center" /> <Setter Property="WidthRequest" Value="80" /> <Setter Property="HeightRequest" Value="80" /> </Style> </ContentPage.Resources> <StackLayout HorizontalOptions="Center" VerticalOptions="Center"> <BoxView> <BoxView.Color> <local:HSLColorExtension H="0" S="1" L="0.5" A="1" /> </BoxView.Color> </BoxView> <BoxView Margin="0,10,0,0"> <BoxView.Color> <local:HSLColor H="0.33" S="1" L="0.5" /> </BoxView.Color> </BoxView> <BoxView Margin="0,10,0,0" Color="{local:HSLColorExtension H=0.67, S=1, L=0.5}" /> <BoxView Margin="0,10,0,0" Color="{local:HSLColor H=0, S=0, L=0.5}" /> <BoxView Margin="0,10,0,0" Color="{local:HSLColor A=0.5}" /> </StackLayout> </ContentPage> |