From b6cb64e4930de9b16309f2d30c6bb0a2177048fd Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Thu, 12 Jan 2017 21:05:41 +0100 Subject: [C] new OnPlatform mechanism (#658) * [C] Obsolete TargetPlatform * [Xaml] support and test the new syntax * blind fix windows platforms --- Xamarin.Forms.Core/Device.cs | 33 ++++++++------ Xamarin.Forms.Core/IPlatformServices.cs | 2 + Xamarin.Forms.Core/ListView.cs | 2 +- Xamarin.Forms.Core/OnPlatform.cs | 75 +++++++++++++++++++++++++++++--- Xamarin.Forms.Core/TargetPlatform.cs | 4 +- Xamarin.Forms.Core/TemplatedItemsList.cs | 4 +- 6 files changed, 97 insertions(+), 23 deletions(-) (limited to 'Xamarin.Forms.Core') diff --git a/Xamarin.Forms.Core/Device.cs b/Xamarin.Forms.Core/Device.cs index 5a02bcea..f18251d2 100644 --- a/Xamarin.Forms.Core/Device.cs +++ b/Xamarin.Forms.Core/Device.cs @@ -8,13 +8,30 @@ namespace Xamarin.Forms { public static class Device { + public const string iOS = "iOS"; + public const string Android = "Android"; + public const string WinPhone = "WinPhone"; + public const string Windows = "Windows"; + internal static DeviceInfo info; static IPlatformServices s_platformServices; public static TargetIdiom Idiom { get; internal set; } - public static TargetPlatform OS { get; internal set; } + [Obsolete("Use RuntimePlatform instead.")] +#pragma warning disable 0618 + public static TargetPlatform OS { + get { + TargetPlatform platform; + if (Enum.TryParse(RuntimePlatform, out platform)) + return platform; + return TargetPlatform.Other; + } + } +#pragma warning restore 0618 + + public static string RuntimePlatform => PlatformServices.RuntimePlatform; internal static DeviceInfo Info { @@ -58,6 +75,7 @@ namespace Xamarin.Forms return GetNamedSize(size, targetElementType, false); } + [Obsolete("Use switch(RuntimePlatform) instead.")] public static void OnPlatform(Action iOS = null, Action Android = null, Action WinPhone = null, Action Default = null) { switch (OS) @@ -88,6 +106,7 @@ namespace Xamarin.Forms } } + [Obsolete("Use switch(RuntimePlatform) instead.")] public static T OnPlatform(T iOS, T Android, T WinPhone) { switch (OS) @@ -104,18 +123,6 @@ namespace Xamarin.Forms return iOS; } - public static T OnPlatform(T iOS, T Android, T WinPhone, T Tizen) - { - if (OS == TargetPlatform.Tizen) - { - return Tizen; - } - else - { - return OnPlatform(iOS, Android, WinPhone); - } - } - public static void OpenUri(Uri uri) { PlatformServices.OpenUriAction(uri); diff --git a/Xamarin.Forms.Core/IPlatformServices.cs b/Xamarin.Forms.Core/IPlatformServices.cs index a01baa02..39f00731 100644 --- a/Xamarin.Forms.Core/IPlatformServices.cs +++ b/Xamarin.Forms.Core/IPlatformServices.cs @@ -28,5 +28,7 @@ namespace Xamarin.Forms void OpenUriAction(Uri uri); void StartTimer(TimeSpan interval, Func callback); + + string RuntimePlatform { get; } } } \ No newline at end of file diff --git a/Xamarin.Forms.Core/ListView.cs b/Xamarin.Forms.Core/ListView.cs index 8b4fc53d..0751e1c2 100644 --- a/Xamarin.Forms.Core/ListView.cs +++ b/Xamarin.Forms.Core/ListView.cs @@ -71,7 +71,7 @@ namespace Xamarin.Forms public ListView([Parameter("CachingStrategy")] ListViewCachingStrategy cachingStrategy) : this() { - if (Device.OS == TargetPlatform.Android || Device.OS == TargetPlatform.iOS) + if (Device.RuntimePlatform == Device.Android || Device.RuntimePlatform == Device.iOS) CachingStrategy = cachingStrategy; } diff --git a/Xamarin.Forms.Core/OnPlatform.cs b/Xamarin.Forms.Core/OnPlatform.cs index 02e27083..68621134 100644 --- a/Xamarin.Forms.Core/OnPlatform.cs +++ b/Xamarin.Forms.Core/OnPlatform.cs @@ -1,16 +1,79 @@ +using System; +using System.Collections.Generic; +using Xamarin.Forms.Xaml; + namespace Xamarin.Forms { + [ContentProperty("Platforms")] public class OnPlatform { - public T Android { get; set; } - public T iOS { get; set; } - public T WinPhone { get; set; } + public OnPlatform() + { + Platforms = new List(); + } + + 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; + } + } - public T Tizen { get; set; } + T winPhone; + [Obsolete] + public T WinPhone { + get { return winPhone; } + set { + useLegacyFallback = true; + winPhone = value; + } + } + + public IList Platforms { get; private set; } + + static readonly IValueConverterProvider s_valueConverter = DependencyService.Get(); public static implicit operator T(OnPlatform onPlatform) { - return Device.OnPlatform(iOS: onPlatform.iOS, Android: onPlatform.Android, WinPhone: onPlatform.WinPhone, Tizen: onPlatform.Tizen); + 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 } } -} \ No newline at end of file + + [ContentProperty("Value")] + public class On + { + [TypeConverter(typeof(ListStringTypeConverter))] + public IList Platform { get; set; } + public object Value { get; set; } + } +} diff --git a/Xamarin.Forms.Core/TargetPlatform.cs b/Xamarin.Forms.Core/TargetPlatform.cs index 62fbda09..6125695f 100644 --- a/Xamarin.Forms.Core/TargetPlatform.cs +++ b/Xamarin.Forms.Core/TargetPlatform.cs @@ -1,5 +1,8 @@ +using System; + namespace Xamarin.Forms { + [Obsolete] public enum TargetPlatform { Other, @@ -7,6 +10,5 @@ namespace Xamarin.Forms Android, WinPhone, Windows, - Tizen } } diff --git a/Xamarin.Forms.Core/TemplatedItemsList.cs b/Xamarin.Forms.Core/TemplatedItemsList.cs index cb64e2e9..a954939d 100644 --- a/Xamarin.Forms.Core/TemplatedItemsList.cs +++ b/Xamarin.Forms.Core/TemplatedItemsList.cs @@ -970,7 +970,7 @@ namespace Xamarin.Forms /* HACKAHACKHACK: LongListSelector on WP SL has a bug in that it completely fails to deal with * INCC notifications that include more than 1 item. */ - if (fixWindows && Device.OS == TargetPlatform.WinPhone) + if (fixWindows && Device.RuntimePlatform == Device.WinPhone) { SplitCollectionChangedItems(e); return; @@ -1205,7 +1205,7 @@ namespace Xamarin.Forms //Hack: the cell could still be visible on iOS because the cells are reloaded after this unhook //this causes some visual updates caused by a null datacontext and default values like IsVisible - if (Device.OS == TargetPlatform.iOS && CachingStrategy == ListViewCachingStrategy.RetainElement) + if (Device.RuntimePlatform == Device.iOS && CachingStrategy == ListViewCachingStrategy.RetainElement) await Task.Delay(100); item.BindingContext = null; } -- cgit v1.2.3