Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Reinventing the Image Button

$
0
0

Hi,

Due to my issues with the ImageButton as present in Labs I thought I would have a go at creating my own, simple version, which is an opportunity to learn further about Custom Controls in Xamarin.Forms.

I have run into an issue regarding the Source of the image.

The problem seems to be related to the following line in the Constructor of my Custom Control :-

myImage.SetBinding(Image.SourceProperty, "Source");

And this line in the View constructor which consumes MyImageButton:-

myImageButtonViewModel.ActionCommandImage = "Settings.png";

I then receive this exception :-

System.InvalidCastException: Value is not a convertible object: System.String to Xamarin.Forms.ImageSource

My Source property in the Custom Control is defined like so :-

public static readonly BindableProperty SourceProperty =
    BindableProperty.Create<MyImageButton, ImageSource>(p => p.Source, null);

[TypeConverter(typeof(MyImageSourceConverter))] 
public ImageSource Source
{
    get { return (ImageSource)GetValue(SourceProperty); }
    set { SetValue(SourceProperty, value); }
}

I have read windingroadway.blogspot.co.uk/2014/06/xamarinforms-custom-controls-imagesource.html and reviewed the Labs source code however I am not sure whether I need to go to the whole trouble of defining a Custom Renderer for each platform when if I were to define the Custom Control directly in the View's XAML then everything would work fine.

Can someone point me in some sort of direction because I am completely lost?

I have tried to keep things a simple as possible, hence why I have only provided key lines of code, however if it would be of benefit for me to share the complete solution then I can do this as a ZIP file.

Thanks for taking the time to read this.

Paul Diston


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>