This extension allows to set image in XAML from resource images.
1. Extension code
[ContentProperty("Source")]2. Add namespace
public class ImageResourceExtension : IMarkupExtension
{
public string Source { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (Source == null)
{
return null;
}
var imageSource = ImageSource.FromResource("MobileApp.Images." + Source);
return imageSource;
}
}
xmlns:resources="clr-namespace:MobileApp.Resources;assembly=MobileApp"3. Usage
<Image Source="{resources:ImageResource image.png}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="Fill" />
No comments:
Post a Comment