Skip to content

Instantly share code, notes, and snippets.

@reniris
Last active February 21, 2018 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reniris/0556bf9c29abed9ccf1782a05f488a84 to your computer and use it in GitHub Desktop.
Save reniris/0556bf9c29abed9ccf1782a05f488a84 to your computer and use it in GitHub Desktop.
XamarinでPickerにEnumをバインド
namespace TaxPon.View.Themes
{
public enum ThemeType
{
Dark,
Light,
}
}
<?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="TaxPon.View.SettingPage"
xmlns:m="clr-namespace:TaxPon.Model"
xmlns:c="clr-namespace:TaxPon.View.Converter"
xmlns:vm="clr-namespace:TaxPon.ViewModel"
Title="EnumBindablePicker">
<ContentPage.Icon>
</ContentPage.Icon>
<ContentPage.Resources>
<ResourceDictionary>
<c:IntEnumConverter x:Key="IntEnum"/>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.BindingContext>
<vm:SettingViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout>
<StackLayout Orientation = "Horizontal" Margin="10, 0, 10, 0">
<Label Text = "EnumPicker" VerticalTextAlignment="Center"/>
<Picker ItemsSource="{Binding Themes}" SelectedIndex="{Binding SelectedTheme.Value, Converter={StaticResource IntEnum}, Mode=TwoWay}" VerticalOptions="End" HorizontalOptions="CenterAndExpand"/>
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
using Reactive.Bindings;
using Reactive.Bindings.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using TaxPon.Model;
using TaxPon.View.Themes;
using Xamarin.Forms;
namespace TaxPon.ViewModel
{
public class SettingViewModel : IDisposable
{
public ReactiveProperty<ThemeType> SelectedTheme { get; }
private CompositeDisposable Disposable { get; } = new CompositeDisposable();
public List<string> Themes { get; } = Enum.GetNames(typeof(ThemeType)).ToList();
public SettingViewModel()
{
//Enum
this.SelectedTheme = new ReactiveProperty<ThemeType>();
.AddTo(this.Disposable);
//Picker変更
this.SelectedTheme.ObserveProperty(x => x.Value)
.Subscribe(x => { //ここにPickerの選択が変更されたときの処理を書く
Console.WriteLine(x);
})
.AddTo(this.Disposable);
}
public void Dispose()
{
this.Disposable.Dispose();
}
}
}
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Xamarin.Forms;
namespace TaxPon.View.Converter
{
public class IntEnumConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Enum)
{
return (int)value;
}
return 0;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is int)
{
return Enum.ToObject(targetType, value);
}
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment