첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.15MB

▶ DoubleToStringConverter.cs

using System.Globalization;

namespace Testproject;

/// <summary>
/// 실수→문자열 변환자
/// </summary>
public class DoubleToStringConverter : IValueConverter
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 변환하기 - Convert(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)
    {
        if(int.TryParse((string)parameter, out int decimalPoint))
        {
            return ((double)sourceValue).ToString($"f{decimalPoint}");
        }
        else
        {
            return ((double)sourceValue).ToString();
        }
    }

    #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>
    public object ConvertBack(object sourceValue, Type targetType, object parameter, CultureInfo cultureInfo)
    {
        throw new NotImplementedException();
    }

    #endregion
}

 

728x90

 

▶ MainPage.xaml

<?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:DoubleToStringConverter x:Key="DoubleToStringConverterKey" />
        <Style TargetType="Label">
            <Setter Property="HorizontalTextAlignment" Value="Center" />
        </Style>
    </ContentPage.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="500"  />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <BoxView x:Name="boxView"
            Color="Black" />
        <Grid Grid.Row="1"
            Margin="20"
            RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto">
            <Slider x:Name="redSlider" />
            <Label Grid.Row="1"
                Text="{Binding Source={x:Reference redSlider},
                    Path=Value,
                    ConverterParameter=2,
                    Converter={StaticResource DoubleToStringConverterKey},
                    StringFormat='적색 : {0}'}" />
            <Slider x:Name="greenSlider" Grid.Row="2" />
            <Label Grid.Row="3"
                Text="{Binding Source={x:Reference greenSlider},
                    Path=Value,
                    ConverterParameter=2,
                    Converter={StaticResource DoubleToStringConverterKey},
                    StringFormat='녹색 : {0}'}" />
            <Slider x:Name="blueSlider" Grid.Row="4" />
            <Label Grid.Row="5"
                Text="{Binding Source={x:Reference blueSlider},
                    Path=Value,
                    ConverterParameter=2,
                    Converter={StaticResource DoubleToStringConverterKey},
                    StringFormat='청색 : {0}'}" />
        </Grid>
    </Grid>
</ContentPage>

 

300x250

 

▶ MainPage.xaml.cs

namespace TestProject;

/// <summary>
/// 메인 페이지
/// </summary>
public partial class MainPage : ContentPage
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 생성자 - MainPage()

    /// <summary>
    /// 생성자
    /// </summary>
    public MainPage()
    {
        InitializeComponent();

        this.redSlider.ValueChanged   += slider_ValueChanged;
        this.greenSlider.ValueChanged += slider_ValueChanged;
        this.blueSlider.ValueChanged  += slider_ValueChanged;
    }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Private

    #region 슬라이더 값 변경시 처리하기 - slider_ValueChanged(sender, e)

    /// <summary>
    /// 슬라이더 값 변경시 처리하기
    /// </summary>
    /// <param name="sender">이벤트 발생자</param>
    /// <param name="e">이벤트 인자</param>
    private void slider_ValueChanged(object sender, ValueChangedEventArgs e)
    {
        this.boxView.Color = Color.FromRgb(this.redSlider.Value, this.greenSlider.Value, this.blueSlider.Value);
    }

    #endregion
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요