Created
February 21, 2019 10:39
-
-
Save MartinZikmund/95f68f4bef884bc7bc6f2d9fb134e33b to your computer and use it in GitHub Desktop.
Improved FileImage markup extension with pseudo-folders
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
[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