Xamarin Forms - how to add background image to content page

I am trying to assign a background image to my content created in xaml. I tried different approaches, but this does not show me the background image. Here is the code I wrote:

Content Page:

<?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="Demo.Welcome"
             BackgroundImage="bg1.jpg">

  <ContentPage.Content>
    <StackLayout  HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" Spacing="25" Padding="0" x:Name="stackLayoutMain">
      <Label FontSize="40" Text="WelCome Page" HorizontalOptions="Start" VerticalOptions="StartAndExpand"></Label>
      <Entry Placeholder="Activation Key" WidthRequest="200" FontSize="15" HorizontalOptions="Center" VerticalOptions="StartAndExpand" TextChanged="entryActivationTextChanged" x:Name="entryActivationKey" Keyboard="Numeric" HorizontalTextAlignment="Center"></Entry>
      <Button
        x:Name="buttonActivate"
        BackgroundColor="#fff"
        Text="Activate"
        WidthRequest="100"
        HeightRequest="50"
        HorizontalOptions="Center"
        VerticalOptions="StartAndExpand"
        TextColor="#377CC1"
        IsVisible="False"
        Clicked="buttonActivateClicked" >
      </Button>
    </StackLayout>
  </ContentPage.Content>
</ContentPage>

bg1.jpg is the image in my portable project that I want to set as the background, and I set the property

  • Build Action = "Content" and
  • Copy to Output Directory = "Copy Always"

And below is the directory structure of my project.

Directory structure

+6
source share
2 answers

My suggestion is to put the images in a folder:

for Android Resources/drawable

for iOS Resources

+13
source
public class MainPage : ContentPage
{
    List<string> objEmpCollections = new List<string>();
    public MainPage()
    {
        this.Title = "V V I P's";
        //this.BackgroundColor = Color.Maroon;
        this.BackgroundImage = "EmpMgmtPCL.Images.NPO-Events-bg.png";
    }
}
+2

Source: https://habr.com/ru/post/1651245/


All Articles