Last active
February 21, 2018 08:31
-
-
Save reniris/0556bf9c29abed9ccf1782a05f488a84 to your computer and use it in GitHub Desktop.
XamarinでPickerにEnumをバインド
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace TaxPon.View.Themes | |
{ | |
public enum ThemeType | |
{ | |
Dark, | |
Light, | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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