xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

[Bug] BackgroundImageSource repeat itself to fill screen #8863

Open mfranc28 opened 4 years ago

mfranc28 commented 4 years ago

When I use BackgroundImageSource on Xaml Page the image don't fill completely the screen and repeat itself to do it. The size of image was 5336x3000

  1. Add backgroundimagesource on Xaml ContentPage

Expected Behavior

Thebackground image fill the screen on every device size screen

Actual Behavior

The image don't fill the screen and repeat itself

Basic Information

IMG_C84A5B77AABD-1

abdul-wasey commented 4 years ago

I was also facing this issue , but Here is a workaround that will solve this issue


<ContentPage.Content>
        <Grid>

            <Image Source="BackGroundImage.jpg" 
                   Aspect="AspectFill"
                   />

            <!-- Place your layouts and controls here, for example-->

            <Frame BackgroundColor="White"
                   CornerRadius="10"
                   Padding="0"
                   Margin="10"
                   VerticalOptions="CenterAndExpand"
                   HeightRequest="150">

                <Label Text="Label in the frame" 
                       TextColor="Black"
                       FontSize="22"
                       HorizontalTextAlignment="Center"
                       VerticalOptions="CenterAndExpand"/>

            </Frame>

        </Grid>
    </ContentPage.Content>
kingces95 commented 4 years ago

Could you please attach a zipped solution file exhibiting the unexpected behavior?

abdul-wasey commented 4 years ago

Could you please attach a zipped solution file exhibiting the unexpected behavior?

This sample project is working good on android emulator nexus 6p (Android 9.0 - API 28) and on iPhoneSimulator (iPhone 8 Plus iOS 13.2) its not covering the background of contentpage as like in Andorid Emulator.

rmarinho commented 4 years ago

We need to add a behavior for the aspect, something like

BackgroundImageAspect to Page

MrBugReporter commented 4 years ago

It's been half an year and no progress on this? I haven't been able to use background page images on any of my projects because of this. Did Xamarin decide to completely ignore this issue?

I was also facing this issue , but Here is a workaround that will solve this issue

<ContentPage.Content>
        <Grid>

            <Image Source="BackGroundImage.jpg" 
                   Aspect="AspectFill"
                   />

            <!-- Place your layouts and controls here, for example-->

            <Frame BackgroundColor="White"
                   CornerRadius="10"
                   Padding="0"
                   Margin="10"
                   VerticalOptions="CenterAndExpand"
                   HeightRequest="150">

                <Label Text="Label in the frame" 
                       TextColor="Black"
                       FontSize="22"
                       HorizontalTextAlignment="Center"
                       VerticalOptions="CenterAndExpand"/>

            </Frame>

        </Grid>
    </ContentPage.Content>

That workaround doesn't work well when you have UseSafeArea as true for iOS.

chrisgull commented 4 years ago

I'm facing the same limitation, using a BackgroundImageSource on a MultiPage. Because a MultiPage only can contain Pages, there is no workaround like using an Image in Content with Aspect property set.

I made a PageRenderer for UWP as a workaround, here is a proof-of-concept code snippet of the renderer. Obviously the image source and aspect should be pulled from the portable code layer. (sorry about the code formatting)

` [assembly: ExportRenderer( typeof( MyContentPage ), typeof( MyContentPageRenderer ) )] namespace myapp.uwp {

class MyContentPageRenderer : PageRenderer
{
    public MyContentPageRenderer( )
    {
    }
    protected override void OnElementChanged( ElementChangedEventArgs<Xamarin.Forms.Page> e )
    {
        base.OnElementChanged( e );
        Background = new ImageBrush
        {
            ImageSource = new BitmapImage( new Uri( "ms-appx:///mypic.png" ) ),
            Stretch = Stretch.UniformToFill,
        };
    }
}

} `