简体   繁体   中英

Xamarin - No resource Id shown or available in image properties

I am trying to embed an image in Xamarin and was expecting to see a resource Id appear in the properties of the image. It seems to be a problem that I cannot find an answer for. I have updated VS, JDK to 64 bit, updated Xamarin, cleaned rebuilt, restarted. I'm using the latest API (API 27 8.1)

I have checked the Xamarin forum and the VS forums and there is no answer available. In my code-behind I am writing the following:

bgImage.Source = ImageSource.FromResource("FoodSnapApps.Images.steps.jpg"); 

Obviously it wont work as the resource has no reference.

在此处输入图像描述

As always, any help will be greatly appreciated.

As stated here , in Xamarin forms local images must be loaded in the platform specific projects :

Image files can be added to each application project and referenced from Xamarin.Forms shared code. To use a single image across all apps, the same filename must be used on every platform, and it should be a valid Android resource name (ie. only lowercase letters, numerals, the underscore, and the period are allowed).

  • iOS - The preferred way to manage and support images since iOS 9 is to use Asset Catalog Image Sets, which should contain all of the versions of an image that are necessary to support various devices and scale factors for an application. For more information, see Adding Images to an Asset Catalog Image Set.
  • Android - Place images in the Resources/drawable directory with Build Action: AndroidResource. High- and low-DPI versions of an image can also be supplied (in appropriately named Resources subdirectories such as drawable-ldpi, drawable-hdpi, and drawable-xhdpi).
  • Windows Phone - Place images in the application's root directory with Build Action: Content.
  • Universal Windows Platform (UWP) - Place images in the application's root directory with Build Action: Content.

Then you can access them as follow:

In XAML: <Image Source="waterfront.jpg" />

In C#: var image = new Image { Source = "waterfront.jpg" };

I suggest you to read all that page of documentation to proper handling images in your Xamarin forms project.

HIH

I know it's an old question, but for people who may reach this page on future, code below works for me:

bgImage.Source="resource://FoodSnapApps.Images.steps.jpg"

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