0

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.

enter image description here

As always, any help will be greatly appreciated.

The OrangeGoblin
  • 764
  • 2
  • 7
  • 27

2 Answers2

2

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

Community
  • 1
  • 1
fmaccaroni
  • 3,846
  • 1
  • 20
  • 35
  • Thanks for that. I'll let you know. I have to read the Xamarin docs. I just dived in to be fair. – The OrangeGoblin Apr 19 '18 at 17:59
  • So I read the documentation regarding images and did several hours of tutorials for images and resources. While I know you gave the right answer the code behind cannot access image resources or files. I have no idea why. I seem to spend more time fixing the IDE than I do actually getting any work done. I am not a fan of Xamarin!!! – The OrangeGoblin Apr 21 '18 at 23:25
  • What do you mean by "the code behind cannot access image resources"? If you are using that code and you set the images in the drawable folders in Android it should work. If you need more help update your question with an image of your solution structure and where are the image resources; also put the code you are using to get the image resource in the code behind – fmaccaroni Apr 22 '18 at 03:05
0

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"

Éder Rocha
  • 1,538
  • 11
  • 29