Skip to content

Instantly share code, notes, and snippets.

@MartinZikmund
Last active February 19, 2019 10:43
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/5ed5b966b431955928e6cee31a3e5da4 to your computer and use it in GitHub Desktop.
Save MartinZikmund/5ed5b966b431955928e6cee31a3e5da4 to your computer and use it in GitHub Desktop.
File image markup extension V1
[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) throw new InvalidOperationException($"Cannot convert null to {typeof(ImageSource)}");
if ( Device.RuntimePlatform == Device.UWP )
{
path = System.IO.Path.Combine("Assets/", path);
}
return (FileImageSource)ImageSource.FromFile(path);
}
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