Created
September 21, 2018 13:36
-
-
Save mackayn/3e33ed226ba26c7ad81850239944cb02 to your computer and use it in GitHub Desktop.
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
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