Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Replacement for Xamarin.Forms.OnPlatform which supports the Windows (WinRT) platforms.
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;
}
}
}
}
@peterfoot

This comment has been minimized.

Copy link
Owner Author

@peterfoot peterfoot commented Oct 13, 2015

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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.