这是一个令人难以置信的 API,我不认为 this forum 中所述的原因证明它的弃用是合理的。
它可以很容易地扩展为支持多个平台。我了解there have 并且在不久的将来会有很多变化,但我认为任何开发人员都不会选择在每次必须做出简单决定时编写繁琐的switch 语句。这样的代码会变得很庞大。
所以,我尝试重新创建Device.OnPlatform<T>
namespace Xamarin.Forms
{
public static class DevicePlatform
{
private static T OnImpl<T>(T @default = default, T iOS = default, T Android = default, Func<T> computeCustom = null)
{
switch (Device.RuntimePlatform)
{
case Device.iOS: return iOS;
case Device.Android: return Android;
default:
if (computeCustom == null) return @default;
return computeCustom.Invoke();
}
}
public static T On<T>(T @default = default, T iOS = default, T Android = default) => OnImpl(@default, iOS, Android, null);
public static T On<T>(T @default = default, T iOS = default, T Android = default, (string Platform, T Value) custom = default)
{
string runtimePlatform = Device.RuntimePlatform;
return OnImpl(@default, iOS, Android, () =>
{
if (custom.Platform == runtimePlatform)
return custom.Value;
return @default;
});
}
public static T On<T>(T @default = default, T iOS = default, T Android = default, (string Platform, T Value) custom = default, (string Platform, T Value) custom2 = default)
{
string runtimePlatform = Device.RuntimePlatform;
return OnImpl(@default, iOS, Android, () =>
{
if (custom.Platform == runtimePlatform)
return custom.Value;
else if (custom2.Platform == runtimePlatform)
return custom2.Value;
return @default;
});
}
public static T On<T>(T @default = default, T iOS = default, T Android = default, (string Platform, T Value) custom = default, (string Platform, T Value) custom2 = default, (string Platform, T Value) custom3 = default)
{
string runtimePlatform = Device.RuntimePlatform;
return OnImpl(@default, iOS, Android, () =>
{
if (custom.Platform == runtimePlatform)
return custom.Value;
else if (custom2.Platform == runtimePlatform)
return custom2.Value;
else if (custom3.Platform == runtimePlatform)
return custom3.Value;
return @default;
});
}
public static T On<T>(T @default = default, T iOS = default, T Android = default, (string Platform, T Value) custom = default, (string Platform, T Value) custom2 = default, (string Platform, T Value) custom3 = default, (string Platform, T Value) custom4 = default)
{
string runtimePlatform = Device.RuntimePlatform;
return OnImpl(@default, iOS, Android, () =>
{
if (custom.Platform == runtimePlatform)
return custom.Value;
else if (custom2.Platform == runtimePlatform)
return custom2.Value;
else if (custom3.Platform == runtimePlatform)
return custom3.Value;
else if (custom4.Platform == runtimePlatform)
return custom4.Value;
return @default;
});
}
public static T On<T>(T @default = default, T iOS = default, T Android = default, params (string Platform, T Value)[] customPlatforms)
{
string runtimePlatform = Device.RuntimePlatform;
return OnImpl(@default, iOS, Android, () =>
{
for (int i = 0; i < customPlatforms.Length; i++)
{
var platform = customPlatforms[i];
if (platform.Platform == runtimePlatform)
return platform.Value;
}
return @default;
});
}
}
}
这是一个如何使用它的示例:
Padding = new Thickness(0, DevicePlatform.On(0, iOS: 20, Android: 0, custom: (Device.Tizen, 34)));
如您所见,我们很酷的 API 又回来了,Tizen 将永远被人们铭记。
我计划用它制作一个微型 nuget 包,因此我愿意接受任何形式的更正和帮助。
干杯