summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/OnPlatform.cs
blob: 686211344bfeeae18f41efea8776768eb78dee80 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections.Generic;
using Xamarin.Forms.Xaml;

namespace Xamarin.Forms
{
	[ContentProperty("Platforms")]
	public class OnPlatform<T>
	{
		public OnPlatform()
		{
			Platforms = new List<On>();
		}

		bool useLegacyFallback;
		T android;
		[Obsolete]
		public T Android {
			get { return android; }
			set {
				useLegacyFallback = true;
				android = value;
			}
		}

		T ios;
		[Obsolete]
		public T iOS {
			get { return ios; }
			set {
				useLegacyFallback = true;
				ios = value;
			}
		}

		T winPhone;
		[Obsolete]
		public T WinPhone {
			get { return winPhone; }
			set {
				useLegacyFallback = true;
				winPhone = value;
			}
		}

		public IList<On> Platforms { get; private set; }

		static readonly IValueConverterProvider s_valueConverter = DependencyService.Get<IValueConverterProvider>();

		public static implicit operator T(OnPlatform<T> onPlatform)
		{
			foreach (var onPlat in onPlatform.Platforms) {
				if (onPlat.Platform == null)
					continue;
				if (!onPlat.Platform.Contains(Device.RuntimePlatform))
					continue;
				if (s_valueConverter == null)
					continue;
				return (T)s_valueConverter.Convert(onPlat.Value, typeof(T), null, null);
			}

			if (!onPlatform.useLegacyFallback)
				return default(T);

			//legacy fallback
#pragma warning disable 0618, 0612
			return Device.OnPlatform(iOS: onPlatform.iOS, Android: onPlatform.Android, WinPhone: onPlatform.WinPhone);
#pragma warning restore 0618, 0612
		}
	}

	[ContentProperty("Value")]
	public class On
	{
		[TypeConverter(typeof(ListStringTypeConverter))]
		public IList<string> Platform { get; set; }
		public object Value { get; set; }
	}
}