1

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
  • 1
    Try removing Resources.Designer.cs and rebuilding. Are you sure the shared project doesn't contain the image anymore? Commented Apr 20, 2016 at 8:17
  • It seems that Resources.Designer.cs moved away from the project, yes. I'll give a shot and let you know Commented Apr 20, 2016 at 8:23
  • Was that, I haven't a Resource.Designer.cs on my project ! Commented Apr 20, 2016 at 8:47

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

And I've rebuild, clean, reboot visual, nothing changes
1

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.

Comments

0

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

3 Comments

The standard Button can also have images.
Unfortunaly, same issue with XLabs. That's why I guess it's not a component issue, but with my project/image
The way you have referred the image is actually correct... It should work. That is why I thought perhaps a different widget might help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.