Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 633
mkkekkonen Avatar asked Jun 17 '16 10:06

mkkekkonen


3 Answers

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

like image 192
arsena Avatar answered Nov 20 '22 00:11

arsena


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>
like image 21
jzeferino Avatar answered Nov 20 '22 02:11

jzeferino


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
like image 1
LeRoy Avatar answered Nov 20 '22 02:11

LeRoy