Created
October 13, 2015 19:25
-
-
Save peterfoot/114be5e8c9816e8eefe0 to your computer and use it in GitHub Desktop.
Replacement for Xamarin.Forms.OnPlatform which supports the Windows (WinRT) platforms.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace InTheHand.Forms | |
{ | |
/// <summary> | |
/// Replacement for Xamarin.Forms.OnPlatform which supports the Windows (WinRT) platforms. | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
public sealed class OnPlatform2<T> | |
{ | |
public OnPlatform2() | |
{ | |
Android = default(T); | |
iOS = default(T); | |
WinPhone = default(T); | |
Windows = default(T); | |
Other = default(T); | |
} | |
public T Android{ get; set;} | |
public T iOS { get; set; } | |
public T WinPhone { get; set; } | |
/// <summary> | |
/// The value to use for WinRT (Windows Phone 8.1 and Windows 8.1). | |
/// </summary> | |
public T Windows { get; set; } | |
/// <summary> | |
/// Currently unused. | |
/// </summary> | |
public T Other { get; set; } | |
public static implicit operator T(InTheHand.Forms.OnPlatform2<T> onPlatform) | |
{ | |
switch(Xamarin.Forms.Device.OS) | |
{ | |
case Xamarin.Forms.TargetPlatform.Android: | |
return onPlatform.Android; | |
case Xamarin.Forms.TargetPlatform.iOS: | |
return onPlatform.iOS; | |
case Xamarin.Forms.TargetPlatform.WinPhone: | |
return onPlatform.WinPhone; | |
case Xamarin.Forms.TargetPlatform.Windows: | |
return onPlatform.Windows; | |
default: | |
return onPlatform.Other; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use OnPlatform2 from XAML add an XML namespace directive e.g.
xmlns:forms="clr-namespace:InTheHand.Forms;assembly=YOURASSEMBLY"
Then use in your XAML wherever you want to provide platform specific values e.g.
<Grid.Padding> <forms:OnPlatform2 x:TypeArguments="Thickness" iOS="5" Android="5" Windows="0,10,0,5"/> </Grid.Padding>