简体   繁体   中英

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 项目

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

代码

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?)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM