summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/KeyboardTypeConverter.cs
blob: 4a93010b7d47c70514bfd7cecf2fa4b27b9fe9f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
using System.Linq;
using System.Reflection;

namespace Xamarin.Forms
{
	public class KeyboardTypeConverter : TypeConverter
	{
		public override object ConvertFromInvariantString(string value)
		{
			if (value != null)
			{
				string[] parts = value.Split('.');
				if (parts.Length == 1 || (parts.Length == 2 && parts[0] == "Keyboard"))
				{
					string keyboard = parts[parts.Length - 1];
					FieldInfo field = typeof(Keyboard).GetFields().FirstOrDefault(fi => fi.IsStatic && fi.Name == keyboard);
					if (field != null)
						return (Keyboard)field.GetValue(null);
					PropertyInfo property = typeof(Keyboard).GetProperties().FirstOrDefault(pi => pi.Name == keyboard && pi.CanRead && pi.GetMethod.IsStatic);
					if (property != null)
						return (Keyboard)property.GetValue(null, null);
				}
			}

			throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", value, typeof(Keyboard)));
		}
	}
}