4

I looked extensively online but I couldn't find anything really specific to help me out in this trouble I am having.

I am trying to make a custom renderer for a Button component, in Xamarin forms. Why am I not using the XLabs ImageButton component? Because it's not compatible with Android 4.0.3, which is my current Android target version.

I also tried writing on my own some code that is that much worthless that I am quite shy to post, unless you really require me to :)

I am in need of this code because, without it, images just fail to load in buttons while trying to use them in any button like this:

<Button Image="someImage.png"/>

I tried following a tutorial, here:

http://blog.falafel.com/learning-xamarin-custom-renderers-in-xamarin-forms/

But I failed adapting it to my needs.

PhillyNJ
  • 3,859
  • 4
  • 38
  • 64
Guido Magrin
  • 551
  • 3
  • 7
  • 20

3 Answers3

4

Are no images being displayed at all? Check your locations of the images. Default location in ios is Resources folder, in android it is resources/drawable and in wp it is the root. If you want one image rather than 3 for each solution, put in to the PCL and set the build property to embedded resource and qualify the name with the namespace it is in. ie... ProjectName.Assets.image.png if it is in the PCL Assets directory. Not sure if this is your problem or not, but figured I would offer it up , just in case.

Russ Fustino
  • 155
  • 1
  • 3
  • 10
4

You can solve this by making an Image tap-able using TapGestureRecognizer. For implementation, go through this: http://www.c-sharpcorner.com/UploadFile/e04e9a/xamarin-forms-image-button-recipe/

Aditya Kumar
  • 134
  • 1
  • 4
1

The proper way to do this is described in Chapter 13 of Charles Petzold's excellent book on Xamarin Forms: https://developer.xamarin.com/guides/xamarin-forms/creating-mobile-apps-xamarin-forms/

My approach to this problem is (different than the book) to use a converter for the image file path. The Button.Image property is a FileImageSource object and it wants a file path. Unfortunately you cannot use embedded resources or a content file in the PCL. You must add an individual image file in each of the iOS, Android and UWP projects. The way I do this is to add the image to the PCL and use linking (option on the add existing file dialog).

So here is my converter approach to the above issue

<Button Image="{Binding Converter={StaticResource FileImageSourceConverter}, ConverterParameter=someImage.png}" />

The static resource...

<ContentPage.Resources>
    <ResourceDictionary>
        <local:FileImageSourceConverter x:Key="FileImageSourceConverter"/>
    </ResourceDictionary>
</ContentPage.Resources>

The converter...

public class FileImageSourceConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string filename = parameter as string;
        switch(Device.RuntimePlatform)
        {
            case Device.iOS:
            case Device.Android:
            default:
                return filename;
            case Device.Windows:
                return Path.Combine("Images", filename);
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

The advantages of this approach is that (1) your XAML is not cluttered up with OnPlatform elements; (2) you don't need to put your images in the root directory of the UWP project; (3) much simpler than using a custom render to solve the same problem as some have suggested.

AQuirky
  • 4,691
  • 2
  • 32
  • 51