J.D.
J.D.

Reputation: 1023

How to add an image to my Xamarin.Forms shared project's resources so that it can be used in both the Android and iOS projects?

I'm building a Xamarin.Forms app for Android and iOS and I want to add images to my Xamarin.Forms shared project so that they are displayed in both the Android and iOS app.

This is how I added them currently:

Xamarin.Forms Project

This is how I'm referencing them in code and dynamically adding them to a Grid element:

Code

But I don't see the image loaded in the Grid when I run the app (with the Android emulator debugger). I get the following errors in my Output window:

[0:] Could not load image named: {0}: Images\Blue-1.png

[0:] FileImageSourceHandler: Could not find image or image file was invalid: File: Images\Blue-1.png

Do I need to add copies of the same images to the ModelT.Android and ModelT.iOS projects' resources too? (I thought the whole point of the main shared project was so I wouldn't have to do that?)

Upvotes: 1

Views: 1460

Answers (1)

Nick Peppers
Nick Peppers

Reputation: 3251

Any images in the shared project will need to be set to EmbeddedResource and the full path will need to be used for the source.

var image = new Image
{
    Source = ImageSource.FromResource("ModelT.Images.****Blue-1.png")
};

More info can be found in the docs in the Embedded Images section.

Upvotes: 1

Related Questions