728x90
반응형
728x170
▶ 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
반응형
그리드형(광고전용)
'C# > MAUI' 카테고리의 다른 글
[C#/MAUI/.NET6] TabbedPage 엘리먼트 : Children 속성을 사용해 Page 컬렉션으로 탭 페이지 채우기 (0) | 2022.05.11 |
---|---|
[C#/MAUI/.NET6] NavigationPage 엘리먼트 : TitleView 첨부 속성 사용하기 (0) | 2022.05.11 |
[C#/MAUI/.NET6] FlyoutPage 엘리먼트 사용하기 (0) | 2022.05.11 |
[C#/MAUI/.NET6] VerticalStackLayout 엘리먼트 : Spacing 속성 사용하기 (0) | 2022.05.11 |
[C#/MAUI/.NET6] HorizontalStackLayout 엘리먼트 : Spacing 속성 사용하기 (0) | 2022.05.11 |
[C#/MAUI/.NET6] IValueConverter 인터페이스 : 실수→문자열 변환자 사용하기 (0) | 2022.05.11 |
[C#/MAUI/.NET6] FlexLayout 엘리먼트 : Grow/Order/Basis 첨부 속성 사용하기 (0) | 2022.05.11 |
[C#/MAUI/.NET6] FlexLayout 엘리먼트 : Direction/AlignItems/JustifyContent 속성을 사용해 항목 나열하기 (0) | 2022.05.11 |
[C#/MAUI/.NET6] AbsoluteLayout 클래스 : SetLayoutFlags/SetLayoutBounds 정적 메소드 사용하기 (0) | 2022.05.10 |
[C#/MAUI/.NET6] AbsoluteLayout 엘리먼트 : LayoutFlags/LayoutBounds 첨부 속성 사용하기 (0) | 2022.05.10 |
댓글을 달아 주세요