Skip to content

Instantly share code, notes, and snippets.

@MartinZikmund
Created February 21, 2019 10:39
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 MartinZikmund/95f68f4bef884bc7bc6f2d9fb134e33b to your computer and use it in GitHub Desktop.
Save MartinZikmund/95f68f4bef884bc7bc6f2d9fb134e33b to your computer and use it in GitHub Desktop.
Improved FileImage markup extension with pseudo-folders
[ContentProperty(nameof(Path))]
public class FileImageExtension : IMarkupExtension<FileImageSource>
{
public string Path { get; set; }
public FileImageSource ProvideValue(IServiceProvider serviceProvider) => Convert(Path);
public static FileImageSource Convert(string path)
{
if (path != null)
{
switch (Device.RuntimePlatform)
{
case Device.UWP:
path = System.IO.Path.Combine("Assets/", path);
break;
default:
path = path.Replace('/', '_');
break;
}
return (FileImageSource)ImageSource.FromFile(path);
}
throw new InvalidOperationException($"Cannot convert null into {typeof(ImageSource)}");
}
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