Razzzz
Razzzz

Reputation: 391

Xamarin.Forms BackgroudImage not showing

I am trying to apply background image to all pages within my Xamarin.Forms app using an application style, but it is not applying the style to the pages.

I tried with this:

App.xaml resources

...
<Application.Resources>
  <ResourceDictionary>
    <Style TargetType="Page">
      <Setter Property="BackgroundImageSource" Value="logo.png"/>
    </Style>
...

MainPage.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"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="FinansovoPregledi.MainPage">
    <StackLayout Spacing="10" VerticalOptions="Center">
        <Button Text="Login" BackgroundColor="#006BE6" x:Name="scanButton" TextColor="WhiteSmoke" Command="{Binding LogInCommand}"/>
    </StackLayout>
</ContentPage>

MainPage.xaml.cs

    public partial class MainPage : ContentPage
    {
        LoginViewModel loginViewModel = new LoginViewModel();
        public MainPage()
        {
            InitializeComponent();
            BindingContext = loginViewModel;

        }
...

This isn't working. In my scenario the background image has a white background with transparency.

Upvotes: 0

Views: 260

Answers (1)

Adam
Adam

Reputation: 1414

ContentPage is derived from Page and for your style to apply to Pages as well as all controls derived from Pages you need to set the ApplyToDerivedTypes property of your style to True.

Your style would need to be

<Style ApplyToDerivedTypes="True" TargetType="Page">
  <Setter Property="BackgroundImageSource" Value="logo.png"/>
</Style>

Another approach would be to set the TargetType to ContentPage

<Style TargetType="ContentPage">
  <Setter Property="BackgroundImageSource" Value="logo.png"/>
</Style>

Upvotes: 3

Related Questions