Skip to content

Instantly share code, notes, and snippets.

@mackayn
Created September 21, 2018 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mackayn/3e33ed226ba26c7ad81850239944cb02 to your computer and use it in GitHub Desktop.
Save mackayn/3e33ed226ba26c7ad81850239944cb02 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Foobar.Helpers
{
public class PlatformImageExtension : IMarkupExtension<string>
{
/// <summary>
/// Image source to use
/// </summary>
public string SourceImage { get; set; }
/// <summary>
/// Return value to the XAML serializer, correct iamge path in this case
/// </summary>
/// <param name="serviceProvider"></param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public string ProvideValue(IServiceProvider serviceProvider)
{
if (SourceImage == null)
{
return null;
}
string imagePath;
switch (Device.RuntimePlatform)
{
case Device.Android:
imagePath = SourceImage;
break;
case Device.iOS:
imagePath = SourceImage + ".png";
break;
case Device.UWP:
imagePath = "Images/" + SourceImage + ".png";
break;
default:
throw new ArgumentOutOfRangeException();
}
return imagePath;
}
object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
{
return ProvideValue(serviceProvider);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment