How to attach image as a background in iOS

Phillip Mobley 51 Reputation points
2021-04-16T13:20:19.553+00:00

Hello all.

I am currently creating an App in Xamarin forms to be cross compatible with iOS and Android. I have an image that I want to be as teh background for the app.

Per all of the MS tutorials, in the Android project, I added the image to the drawable folder. For the iOS, I added the image to the Assets catalog using an image set. I placed the image in the 1x slot.

Now, my page is setup like this:

public partial class MainPage : ContentPage
    {
public MainPage()
        {
            InitializeComponent();

            this.BackgroundImageSource = (Device.RuntimePlatform == Device.Android) ? ImageSource.FromFile("SADBackground.png") : ImageSource.FromFile("BackgroundSAD");


        }
}

In Android, everything loads fine. I see the background image. In iOS, I get a white page. Nothing. I have tried many different things and I am unable to get the image to appear in iOS.

So my question is, what is the correct way to reference an image for it to work in iOS? Currently, the image name is SADBackground.png while the Image Set name is BackgroundSAD. Not sure if this matters. I did try it so that the image set name and picture name are the same however, that did not work.

I should also note that the images resolution is 1440x2880

Edit:

I have also decreased the image resolution to match the screen resolution of the iPhone 8 again, no image is being displayed. This is ridiculous.

Developer technologies .NET Xamarin
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-04-19T06:52:54.713+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    What is your name of Image in the Asset folder of iOS? I set the image name to BackgroundSAD in the Asset folder, then use your code.

       this.BackgroundImageSource = (Device.RuntimePlatform == Device.Android) ? ImageSource.FromFile("SADBackground.png") : ImageSource.FromFile("BackgroundSAD");  
    

    Here is running screenshot. It is running normally,

    88938-image.png

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.