I am trying to set a background image for my entire grid, not just for a single row/column. Setting BackgroundImage= "image.jpg"
works on android but on iOS it appears stretched. I've attached a screenshot from the android side to be more clear of how it should be.
Code
<?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="LoyaltyWorx.GridMenu"
Title="Main Menu"
>
<Grid>
<Image Source="grid.jpg"></Image>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="2*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Label Text="Cards" TextColor="White" FontAttributes="Bold" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Grid.Row="0" Grid.Column="0" BackgroundColor="#1481BA" Opacity="0.5" x:Name="CardTile"/>
<Label Text="Transactions" TextColor="Black" FontAttributes="Bold" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Grid.Row="0" Grid.Column="1" BackgroundColor="#ede890" Opacity="0.5" x:Name="TransactionTile"/>
<Label Text="Promotions" TextColor="White" FontSize="30" FontAttributes="Bold" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Grid.Row="1" Grid.Column="0" BackgroundColor="#1481BA" Grid.ColumnSpan="2" Opacity="0.7" x:Name="PromoTile"/>
<Label Text="Settings" TextColor="Black" FontAttributes="Bold" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Grid.Row="2" Grid.Column="0" BackgroundColor="#ede890" Opacity="0.5" x:Name="SettingsTile" />
<Label Text="My Profile" TextColor="White" FontAttributes="Bold" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Grid.Row="2" Grid.Column="1" BackgroundColor="#1481BA" Opacity="0.7" x:Name="ProfileTile"/>
</Grid>
</ContentPage>
As you can see in the above code I've tried to add but all this does is include the image on the "Cards" block. How can I set it behind the entire grid?
You have several options here.
The
Image
within theGrid
The first option is very close to what you tried: Placing the background image in the
Grid
.Please note that XAML is declarative and not sequential. Just because you've placed the
Grid.RowDefinitions
and theGrid.ColumnDefinitions
after theImage
this does not mean that they don't apply to it.You will still have to set
Grid.RowSpan="3"
andGrid.ColumnSpan="2"
in theImage
tag.This will work as long as there is no
Padding
in theGrid
. If you define theGrid
with aPadding
greater zero theImage
will not stretch to the whole parent view, but only the region within the padding. This leads us to the second option: Absolute layout.Using an
AbsoluteLayout
With an
AbsoluteLayout
(see the docs here) you are (to some extent) a bit more flexible (but it comes with its own quirks). Basically you are wrapping theGrid
in anAbsoluteLayout
and place theImage
behind theGrid
.