Xamarin.Forms + ReactiveProperty로 ColorChooser 앱 만들기
17866 단어 XamarinReactiveProperty
할 일
이런 느낌으로 RGB 슬라이더를 움직이면 색이 바뀌는 같은 앱을 만들어 본다.
보통으로 만들면
NotifyPropertyChanged
를 구사해 값의 변화를 각 프로퍼티에 전파시킬 필요가 있다고 생각합니다만, ReactivePropertyView (XAML + C#)
using System;
using System.Collections.Generic;
using ReactiveColorChooser.ViewModels;
using Xamarin.Forms;
namespace ReactiveColorChooser.Views
{
public partial class ColorChooserPage : ContentPage
{
public ColorChooserPage()
{
InitializeComponent();
Title = "Reactive Color Chooser";
BindingContext = new ColorChooserViewModel();
Red.SetBinding<ColorChooserViewModel>(Slider.ValueProperty, vm => vm.Red.Value);
Green.SetBinding<ColorChooserViewModel>(Slider.ValueProperty, vm => vm.Green.Value);
Blue.SetBinding<ColorChooserViewModel>(Slider.ValueProperty, vm => vm.Blue.Value);
MixedColor.SetBinding<ColorChooserViewModel>(BoxView.BackgroundColorProperty, vm => vm.MixedColor.Value);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ReactiveColorChooser.Views.ColorChooserPage">
<ContentPage.Content>
<StackLayout Padding="10, 20">
<StackLayout>
<Label Text="{Binding IntRed.Value}" TextColor="Red" />
<Slider x:Name="Red" Maximum="1" Minimum="0" Value="0" />
<Label Text="{Binding IntGreen.Value}" TextColor="Green" />
<Slider x:Name="Green" Maximum="1" Minimum="0" Value="0" />
<Label Text="{Binding IntBlue.Value}" TextColor="Blue" />
<Slider x:Name="Blue" Maximum="1" Minimum="0" Value="0" />
</StackLayout>
<StackLayout Orientation="Vertical" Padding="0, 20, 0, 0">
<BoxView x:Name="MixedColor" />
<Label Text="{Binding HexColor.Value}" TextColor="{Binding MixedColor.Value}" FontSize="20" />
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
ViewModel
using System;
using System.Reactive.Linq;
using Reactive.Bindings;
using Xamarin.Forms;
namespace ReactiveColorChooser.ViewModels
{
public class ColorChooserViewModel
{
public ReactiveProperty<double> Red { get; private set; }
public ReactiveProperty<int> IntRed { get; private set; }
public ReactiveProperty<double> Green { get; private set; }
public ReactiveProperty<int> IntGreen { get; private set; }
public ReactiveProperty<double> Blue { get; private set; }
public ReactiveProperty<int> IntBlue { get; private set; }
public ReactiveProperty<Color> MixedColor { get; private set; }
public ReactiveProperty<string> HexColor { get; private set; }
public ColorChooserViewModel()
{
Red = new ReactiveProperty<double>(0.0f);
Green = new ReactiveProperty<double>(0.0f);
Blue = new ReactiveProperty<double>(0.0f);
// スライダーの値を0 - 255の範囲の整数に変換
IntRed = Observable.Select(Red, DoubleToInt).ToReactiveProperty();
IntGreen = Observable.Select(Green, DoubleToInt).ToReactiveProperty();
IntBlue = Observable.Select(Blue, DoubleToInt).ToReactiveProperty();
// スライダーの値から色を生成
MixedColor = Observable.CombineLatest(IntRed, IntGreen, IntBlue, (r, g, b) => Color.FromRgb(r, g, b)).ToReactiveProperty();
// 現在の色を16進数表示
HexColor = Observable.CombineLatest(IntRed, IntGreen, IntBlue, (r, g, b) => string.Format(@"#{0}{1}{2}", r.ToString("X2"), g.ToString("X2"), b.ToString("X2"))).ToReactiveProperty();
}
private int DoubleToInt(double x)
{
return (int)(x * 255);
}
}
}
ReactiveProperty<T>
형태로 선언하는 것으로, 마음대로 값의 변화를 감시해 줍니다. 멋지다! ToReactiveProperty
에서 ReactiveProperty 형식으로 되돌리는 것을 잊지 마세요. 받은 값에 대해서는 여러가지 처리를 할 수 있으므로, 이쪽의 조견표를 참고로 하면서 쓰면 좋다고 생각합니다. 매우 도움을 주셔서 감사합니다.
Reactive Extensions 재입문 번외편 「System.Reactive.Linq.Observable의 메소드 일람과 메모」
C#의 초보자로는, 「제대로 변환되고 있는지 모르겠다ー」라고 할 때는
Do
Ruby로 말한다 tap
같은 것 같네요.// 例: 値を変換するたびにログ表示
IntRed = Observable.Select(Red, DoubleToInt).Do(x => System.Diagnostics.Debug.WriteLine(x)).ToReactiveProperty();
후기
리액티브, 좋아!
참고 : MVVM을 리액티브 프로그래밍으로 편안하게 ReactiveProperty 오버뷰
Reference
이 문제에 관하여(Xamarin.Forms + ReactiveProperty로 ColorChooser 앱 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/mah_lab/items/330cf6577ea9e6dcf63e텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)