Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Forms ContentPage.BackgroundImage

  • Xamarin.Forms
  • VS2013 IDE

I know there are other topics on this, but they don't answer my problem.

For Android, the background image works fine.

BackgroundImage = Device.OnPlatform("Resources/[email protected]",
                                    "Drawable/HTMobile.png",
                                    "Images/HTMobile.png");

For iOS, I can't get it to work.

  • I've tried the normal and retina versions of the image, which are of the correct dimensions for those screens.
  • I've tried placing them in the root folder of the project
  • I've tried images with transparent or solid colored image backgrounds

In iOS, the background image, in any of the scenarios above, just shows a white screen ontop of all the CustomContentPage controls. I comment out the background line of code, and my page shows normally, with all controls and navigations links...

With the following code (no 'Resouces' folder), the page shows normally, but no image...

BackgroundImage = Device.OnPlatform("[email protected]",
                                    "Drawable/HTMobile.png",
                                    "Images/HTMobile.png");

How do you set the background image in iOS? For this answer, I need to know the following:

  • Where do you put the image in the project so it's picked up properly?
  • What type of image do you use? Size? Resolution? Color depth? Transparency?
  • How do you let iOS 'know' what image to use if it's a 4s, 5s or 6/6+ iPhone?

EDIT

ContentPage.BackgroundImage is of type FileImageSource - meaning the correct resolution is loaded automatically; therefore the call should be...

BackgroundImage = Device.OnPlatform("HTbg.png",
                                    "Drawable/HTMobile.png",
                                    "Images/HTMobile.png");

But this still doesn't work. I'll add in the 4-inch image version and see if that helps...

like image 497
Robert Achmann Avatar asked Aug 29 '26 16:08

Robert Achmann


1 Answers

This problem on IOS is solved by adding the required image to an IOS asset catalog. Xamarin.Forms WILL NOT load an image into the BackgroundImage property when it is located in the Resources folder as for versions of IOS pre 7. This might save someone some time.

like image 141
Graham Moore Avatar answered Sep 01 '26 04:09

Graham Moore