ColorViewModel
using System.Drawing;
namespace MVVMEnumDemo
{
public class ColorViewModel
{
public Color Color { get; set; }
public string DisplayName { get; set; }
}
}
EnumDemoViewModelusing System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using Xamarin.Forms.Internals;
using System.Linq;
namespace MVVMEnumDemo
{
public class EnumDemoViewModel : INotifyPropertyChanged
{
public EnumDemoViewModel()
{
this.ColorsList = new List<ColorViewModel>()
{
new ColorViewModel(){ Color = Color.Gray , DisplayName = "灰色"} ,
new ColorViewModel(){ Color = Color.Red , DisplayName = "紅色"} ,
new ColorViewModel(){ Color = Color.Green , DisplayName = "綠色"} ,
new ColorViewModel(){ Color = Color.Yellow , DisplayName = "黃色"}
};
// 初始化顏色
SelectedColor = Color.Black;
}
public IEnumerable<ColorViewModel> ColorsList { get; private set; }
/// <summary>
/// 資料來源
/// </summary>
private Color _selectedcolor;
public Color SelectedColor
{
get { return _selectedcolor; }
set
{
_selectedcolor = value;
OnPropertyChanged(nameof(SelectedColor));
}
}
/// <summary>
/// 負責 Picker Index 轉換
/// </summary>
public int SelectedIndex
{
get { return this.ColorsList.IndexOf(c => c.Color == this.SelectedColor); }
set
{
this.SelectedColor = this.ColorsList.ElementAt(value).Color;
OnPropertyChanged(nameof(SelectedIndex));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Xaml View 配置<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MVVMEnumDemo"
mc:Ignorable="d"
x:Class="MVVMEnumDemo.MainPage">
<ContentPage.BindingContext>
<local:EnumDemoViewModel/>
</ContentPage.BindingContext>
<StackLayout Margin="20,20,20,20">
<Picker
FontSize="Large"
ItemsSource="{Binding ColorsList}"
ItemDisplayBinding="{Binding DisplayName}"
SelectedIndex="{Binding SelectedIndex,Mode=TwoWay}"/>
<BoxView Color="{Binding SelectedColor}"
CornerRadius="10"
WidthRequest="300"
HeightRequest="300"
VerticalOptions="Center"
HorizontalOptions="Center" />
</StackLayout>
</ContentPage>
啟動畫面
選擇顏色
原以為 SelectedIndex 會有對應的 Command 可以使用,會來才發現沒有,不是每個事件都會有對應的 Command
- 延伸閱讀
- [X.Form] Picker
![[X.Form] MVVM - Picker & Enum-1](https://live.staticflickr.com/65535/50277096441_2f8664d1fb.jpg)
![[X.Form] MVVM - Picker & Enum-2](https://live.staticflickr.com/65535/50276418798_9c4318d9a3.jpg)
![[X.Form] MVVM - 顏色變化-1](https://live.staticflickr.com/65535/50273188761_74998977b6_w.jpg)
![[X.Form] MVVM - 顏色變化-2](https://live.staticflickr.com/65535/50273352817_a39306b19b_w.jpg)
![[X.Form] MVVM - 顏色變化-3](https://live.staticflickr.com/65535/50272513123_84da1265a3_w.jpg)
![[X.Form] System.TypeLoadException-1](https://live.staticflickr.com/65535/50268340768_445a9f4073_w.jpg)
![[X.Form] System.TypeLoadException-2](https://live.staticflickr.com/65535/50269183817_ebfe91b9ff_z.jpg)
![[X.Form] Android ClearText 設定-1](https://live.staticflickr.com/65535/50255512676_c5ed2332f9_z.jpg)
![[X.Form] Android ClearText 設定-2](https://live.staticflickr.com/65535/50255523931_ddb3d185b5_z.jpg)
![[X.Form] Android ClearText 設定-3](https://live.staticflickr.com/65535/50255705392_cdd6b3bedd_z.jpg)
![[X.Form] Android ClearText 設定-4](https://live.staticflickr.com/65535/50255705357_9474ae1ef1_z.jpg)




