Skip to content

Instantly share code, notes, and snippets.

@conceptdev
Created June 9, 2014 23:06
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save conceptdev/d906c4538a137932fcf6 to your computer and use it in GitHub Desktop.
Save conceptdev/d906c4538a137932fcf6 to your computer and use it in GitHub Desktop.
Xamarin.Forms custom markup extension because there is no built-in TypeConverter for Images embedded as resources
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;
}
}
}
<?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>
@danvanderboom
Copy link

Thanks!

@se7ensoft
Copy link

Unreliable/Hit-or-miss. Sometimes this works, other times it doesn't - even within the same debug session.

@jonathanpeppers
Copy link

@se7ensoft, I'm curious on how this is unreliable. Is the IMarkupExtension unreliable or ImageSource.FromResource? All of the above is working for me, and I'm doing things more complicated than this in our application.

I think it could be more likely the Xamarin.Android debugger is messing with you. Make sure you have linking turned off, because breakpoints are unreliable if you have linking turned on in a Forms app.

@NearChaos
Copy link

I have tried a million combinations and can't get the XAML preview to recognize the extension. I have the forms component as a shared library with a separate Android app so I tried "assembly=MyApp" and "assembly=MyApp.Droid". I tried simplifying (temporarily) the value provider to always return NULL. All kinds of namespace experiments. Absolutely nothing makes this work. I've seen it work exactly zero times. On the current latest Visual Studio Community 7.1.5 for OSX, with the latest Android SDK.

@eltiare
Copy link

eltiare commented Oct 19, 2017

I can't get this to work on Android or iOS, either. :(

@vcartera81
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment