Skip to content
Permalink
Browse files

Added OnPlatform markup extension supporting iOS/Android/UWP for xama…

  • Loading branch information...
ahoefling committed May 4, 2018
1 parent b6eed7f commit f06640b2db9aabe97cad3b64f71006f86705422a
@@ -18,6 +18,8 @@ public object Parse(string match, ref string remaining, IServiceProvider service
markupExtension = new TemplateBindingExtension();
else if (match == "StaticResource")
markupExtension = new StaticResourceExtension();
else if (match == "OnPlatform")
markupExtension = new OnPlatformExtension();
else
{
if (typeResolver == null)
@@ -27,7 +29,7 @@ public object Parse(string match, ref string remaining, IServiceProvider service
//The order of lookup is to look for the Extension-suffixed class name first and then look for the class name without the Extension suffix.
if (!typeResolver.TryResolve(match + "Extension", out type) && !typeResolver.TryResolve(match, out type))
{
var lineInfoProvider = serviceProvider.GetService(typeof (IXmlLineInfoProvider)) as IXmlLineInfoProvider;
var lineInfoProvider = serviceProvider.GetService(typeof(IXmlLineInfoProvider)) as IXmlLineInfoProvider;
var lineInfo = (lineInfoProvider != null) ? lineInfoProvider.XmlLineInfo : new XmlLineInfo();
throw new XamlParseException(String.Format("MarkupExtension not found for {0}", match), lineInfo);
}
@@ -0,0 +1,34 @@
using System;
using System.Xml;

namespace Xamarin.Forms.Xaml
{
public class OnPlatformExtension : IMarkupExtension
{
public object iOS { get; set; }
public object Android { get; set; }
public object UWP { get; set; }

public object ProvideValue(IServiceProvider serviceProvider)
{
if (iOS == null && Android == null && UWP == null)
{
IXmlLineInfoProvider lineInfoProvider = serviceProvider.GetService(typeof(IXmlLineInfoProvider)) as IXmlLineInfoProvider;
IXmlLineInfo lineInfo = (lineInfoProvider != null) ? lineInfoProvider.XmlLineInfo : new XmlLineInfo();
throw new XamlParseException("OnPlatformExtension requires Value property to be set", lineInfo);
}

switch (Device.RuntimePlatform)
{
case Device.iOS:
return iOS;
case Device.Android:
return Android;
case Device.UWP:
return UWP;
}

return null;
}
}
}

2 comments on commit f06640b

@ChaseFlorell

This comment has been minimized.

Copy link

replied May 5, 2018

What about having a default string that allows for custom defined platforms?

<Label FontSize="{OnPlatform iOS=21, Android=20, 'FooBar=18, FizzBuzz=17'"} />
@ahoefling

This comment has been minimized.

Copy link
Owner Author

replied May 5, 2018

Not sure exactly how I would implement that. I can see having merit to having a Default attribute in case the switch statement fails it has something to fallback to.

<Label FontSize="{OnPlatform iOS=21, Android=20, Default=30}" />

Then the file would look like this:

public class OnPlatformExtension : IMarkupExtension
{
	public object iOS { get; set; }
	public object Android { get; set; } 
	public object UWP { get; set; }
        public object Default { get; set; }

	public object ProvideValue(IServiceProvider serviceProvider)
	{
		if (iOS == null && Android == null && UWP == null)
		{
			IXmlLineInfoProvider lineInfoProvider = serviceProvider.GetService(typeof(IXmlLineInfoProvider)) as IXmlLineInfoProvider;
			IXmlLineInfo lineInfo = (lineInfoProvider != null) ? lineInfoProvider.XmlLineInfo : new XmlLineInfo();
			throw new XamlParseException("OnPlatformExtension requires Value property to be set", lineInfo);
		}

		switch (Device.RuntimePlatform)
		{
			case Device.iOS:
				return iOS;
			case Device.Android:
				return Android;
			case Device.UWP:
				return UWP;
		}

		return Default;
	}
}
Please sign in to comment.
You can’t perform that action at this time.