mkkekkonen
mkkekkonen

Reputation: 1774

Xamarin.Forms Page BackgroundImage property

How are you supposed to set the background image for a Page, as the BackgroundImage is a string? I would greatly appreciate any suggestions.

So far I've tried:

MainPage = new ContentPage 
{
    BackgroundImage = "Images/image.png"
}

which does not work. The image file is located in the PCL project.

Upvotes: 22

Views: 52065

Answers (3)

LeRoy
LeRoy

Reputation: 4436

To set the image on a page:

<Image Source="bg"></Image>

Now you need to add your image on each platform:

iOS

Test.iOS > Resources > bg.png

Android

Test.Droid > Resources > bg.png

Upvotes: 1

jzeferino
jzeferino

Reputation: 7850

If you need a solution that allows you to change the AspectRatio and adjust the image you can use this:

XAML:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Pages.PhotoPage">
        <Grid >
            <Image Source="background.png" Aspect="AspectFit" />
            <!-- Place here the rest of the layout for the page. -->
        </Grid >
</ContentPage>

Upvotes: 14

arsena
arsena

Reputation: 1975

If I'm not mistaken you can't share resources. you must put the image in Platform Specific folder and than use BackgroundImage = "image.png" without Images/

EDIT:

It seems I was partly mistaken.

It is possible to share images by embedding them instead of having multiple copies for different Platforms: https://developer.xamarin.com/guides/xamarin-forms/working-with/images/#Embedded_Images

Upvotes: 16

Related Questions