2017-01-12 23:05:41 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using Xamarin.Forms.Xaml;
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
namespace Xamarin.Forms
|
|
|
|
{
|
2017-01-12 23:05:41 +03:00
|
|
|
[ContentProperty("Platforms")]
|
2016-03-22 23:02:25 +03:00
|
|
|
public class OnPlatform<T>
|
|
|
|
{
|
2017-01-12 23:05:41 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2017-01-12 23:05:41 +03:00
|
|
|
T winPhone;
|
|
|
|
[Obsolete]
|
|
|
|
public T WinPhone {
|
|
|
|
get { return winPhone; }
|
|
|
|
set {
|
|
|
|
useLegacyFallback = true;
|
|
|
|
winPhone = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 21:06:29 +03:00
|
|
|
bool hasDefault;
|
|
|
|
T @default;
|
|
|
|
public T Default {
|
|
|
|
get { return @default; }
|
|
|
|
set {
|
|
|
|
hasDefault = true;
|
|
|
|
@default = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 23:05:41 +03:00
|
|
|
public IList<On> Platforms { get; private set; }
|
|
|
|
|
2017-04-25 21:06:29 +03:00
|
|
|
#pragma warning disable RECS0108 // Warns about static fields in generic types
|
2017-01-12 23:05:41 +03:00
|
|
|
static readonly IValueConverterProvider s_valueConverter = DependencyService.Get<IValueConverterProvider>();
|
2017-04-25 21:06:29 +03:00
|
|
|
#pragma warning restore RECS0108 // Warns about static fields in generic types
|
2016-12-15 00:24:59 +03:00
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
public static implicit operator T(OnPlatform<T> onPlatform)
|
|
|
|
{
|
2017-01-12 23:05:41 +03:00
|
|
|
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)
|
2017-04-25 21:06:29 +03:00
|
|
|
return onPlatform.hasDefault ? onPlatform.@default : default(T);
|
2017-01-12 23:05:41 +03:00
|
|
|
|
|
|
|
//legacy fallback
|
|
|
|
#pragma warning disable 0618, 0612
|
|
|
|
return Device.OnPlatform(iOS: onPlatform.iOS, Android: onPlatform.Android, WinPhone: onPlatform.WinPhone);
|
|
|
|
#pragma warning restore 0618, 0612
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
}
|
2017-01-12 23:05:41 +03:00
|
|
|
|
|
|
|
[ContentProperty("Value")]
|
|
|
|
public class On
|
|
|
|
{
|
|
|
|
[TypeConverter(typeof(ListStringTypeConverter))]
|
|
|
|
public IList<string> Platform { get; set; }
|
|
|
|
public object Value { get; set; }
|
|
|
|
}
|
|
|
|
}
|