Skip to content

Instantly share code, notes, and snippets.

@aimore
Created May 30, 2019 02:00
Show Gist options
  • Save aimore/5b2048a1f2c29f75726567dbb53b8728 to your computer and use it in GitHub Desktop.
Save aimore/5b2048a1f2c29f75726567dbb53b8728 to your computer and use it in GitHub Desktop.
Image source extension xamarin
[ContentProperty(nameof(Source))]
public class ImageResourceExtension : IMarkupExtension
{
public string Source { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (Source == null)
{
return null;
}
// Do your translation lookup here, using whatever method you require
var imageSource = ImageSource.FromResource(Source, typeof(ImageResourceExtension).GetTypeInfo().Assembly);
return imageSource;
}
public static string ImageNameFromResource(string u)
{
var assembly = typeof(App).GetTypeInfo().Assembly;
foreach (var res in assembly.GetManifestResourceNames())
{
// System.Diagnostics.Debug.WriteLine("found resource: " + res);
if (res.Contains(u))
{
return res;
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment