1
votes

I'm trying to do an image button on my Xamarin Forms application (shared project configuration).

My XAML is like this :

<Button x:Name="btn_params" Image="app_params.png" Grid.Row="2" Grid.Column="0" Command="{Binding OpenParamsCommand}" HorizontalOptions="StartAndExpand"></Button>

Because I used shared project configuration, it seems like I cannot have my image within my shared project, so I moved it in the appropriate folder. I'm currently only testing on Android.

I put it in Resources/Drawable on my Android

enter image description here

This image is marked as compile as AndroidResource, just like Xamarin says in its documentation

When I open this page, I have an NullReferenceException. I know this is this image, because if I remove the Image param, no errors are throwns...

enter image description here

Any idea of what happens because I have no debug infos other than this exception.

3
Try removing Resources.Designer.cs and rebuilding. Are you sure the shared project doesn't contain the image anymore? - stefana
It seems that Resources.Designer.cs moved away from the project, yes. I'll give a shot and let you know - cdie
Was that, I haven't a Resource.Designer.cs on my project ! - cdie

3 Answers

1
votes

Make sure your image's Build Action is set to Android Resource. You can find the setting by right clicking the file in the Solution Explorer.

Also, do a Clean/Rebuild (and perhaps reinstall the app) to ensure that all artefacts are deployed properly.

1
votes

As said in comments of my question, it was because I haven't any Designer.Resource.cs on my Resource folder in my Android project. Adding one just fix the problem.

0
votes

You will need to use a custom control to have an ImageButton in Xamarin.Forms . XLabs has got a nice widget to have buttons with images. After adding the nuget package, you can refer the widget in XAML like below,

xmlns:controls="clr-namespace:XLabs.Forms.Controls;assembly=XLabs.Forms"
<controls:ImageButton Text="Twitter" BackgroundColor="#01abdf" TextColor="#ffffff" HeightRequest="75" WidthRequest="175" Image="app_params" Grid.Row="2" Grid.Column="0" Command="{Binding OpenParamsCommand}" HorizontalOptions="StartAndExpand" />