Xamarin.Forms custom markup extension because there is no built-in TypeConverter for Images embedded as resources
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
using System; | |
using Xamarin.Forms.Xaml; | |
using Xamarin.Forms; | |
namespace WorkingWithImages | |
{ | |
// You exclude the 'Extension' suffix when using in Xaml markup | |
[ContentProperty ("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); | |
return imageSource; | |
} | |
} | |
} |
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
<?xml version="1.0" encoding="UTF-8" ?> | |
<ContentPage | |
xmlns="http://xamarin.com/schemas/2014/forms" | |
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | |
xmlns:local="clr-namespace:WorkingWithImages;assembly=WorkingWithImages" | |
x:Class="WorkingWithImages.EmbeddedImagesXaml"> | |
<ContentPage.Padding> | |
<OnPlatform x:TypeArguments="Thickness"> | |
<OnPlatform.iOS>0, 20, 0, 0</OnPlatform.iOS> | |
</OnPlatform> | |
</ContentPage.Padding> | |
<StackLayout VerticalOptions="Center" HorizontalOptions="Center"> | |
<Label Text="Image Resource Xaml" /> | |
<!-- uses a custom Extension defined in this project for now --> | |
<Image Source="{local:ImageResource WorkingWithImages.beach.jpg}" /> | |
<Label Text="WorkingWithImages.beach.jpg embedded resource" /> | |
</StackLayout> | |
</ContentPage> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this code just doesn't work for me. I did not get a breakpoint hit neither in the get/set of the property nor in the ProvideValue method