cdie
cdie

Reputation: 4544

Xamarin Button Image not working (NullReferenceException)

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.

Upvotes: 1

Views: 2359

Answers (3)

cdie
cdie

Reputation: 4544

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.

Upvotes: 1

Nikola Irinchev
Nikola Irinchev

Reputation: 2186

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.

Upvotes: 1

Sreeraj
Sreeraj

Reputation: 2444

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" />

Upvotes: 0

Related Questions