summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core
diff options
context:
space:
mode:
authorStephane Delcroix <stephane@delcroix.org>2017-01-12 20:05:41 (GMT)
committerJason Smith <jason.smith@xamarin.com>2017-01-12 20:05:41 (GMT)
commitb6cb64e4930de9b16309f2d30c6bb0a2177048fd (patch)
treeae15f36d3e0e60cac6305f90520ad778de9c6ee3 /Xamarin.Forms.Core
parent3af99cbbe145a876cc9839af0adead83695b5445 (diff)
downloadxamarin-forms-b6cb64e4930de9b16309f2d30c6bb0a2177048fd.zip
xamarin-forms-b6cb64e4930de9b16309f2d30c6bb0a2177048fd.tar.gz
xamarin-forms-b6cb64e4930de9b16309f2d30c6bb0a2177048fd.tar.bz2
[C] new OnPlatform mechanism (#658)
* [C] Obsolete TargetPlatform * [Xaml] support and test the new syntax * blind fix windows platforms
Diffstat (limited to 'Xamarin.Forms.Core')
-rw-r--r--Xamarin.Forms.Core/Device.cs33
-rw-r--r--Xamarin.Forms.Core/IPlatformServices.cs2
-rw-r--r--Xamarin.Forms.Core/ListView.cs2
-rw-r--r--Xamarin.Forms.Core/OnPlatform.cs75
-rw-r--r--Xamarin.Forms.Core/TargetPlatform.cs4
-rw-r--r--Xamarin.Forms.Core/TemplatedItemsList.cs4
6 files changed, 97 insertions, 23 deletions
diff --git a/Xamarin.Forms.Core/Device.cs b/Xamarin.Forms.Core/Device.cs
index 5a02bce..f18251d 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>(T iOS, T Android, T WinPhone)
{
switch (OS)
@@ -104,18 +123,6 @@ namespace Xamarin.Forms
return iOS;
}
- public static T OnPlatform<T>(T iOS, T Android, T WinPhone, T Tizen)
- {
- if (OS == TargetPlatform.Tizen)
- {
- return Tizen;
- }
- else
- {
- return OnPlatform<T>(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 a01baa0..39f0073 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<bool> 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 8b4fc53..0751e1c 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 02e2708..6862113 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<T>
{
- public T Android { get; set; }
- public T iOS { get; set; }
- public T WinPhone { get; set; }
+ 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;
+ }
+ }
- public T Tizen { get; set; }
+ 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)
{
- 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<string> Platform { get; set; }
+ public object Value { get; set; }
+ }
+}
diff --git a/Xamarin.Forms.Core/TargetPlatform.cs b/Xamarin.Forms.Core/TargetPlatform.cs
index 62fbda0..6125695 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 cb64e2e..a954939 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;
}