DoctorWho
DoctorWho

Reputation: 1116

Xamarin - how to make background transparent?

I have a simple page with a black background. I would like to make it transparent so that the page below is visible but blurred.

Someone suggest :

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:NameSpace"
         x:Class="NameSpace.MainPage"
         BackgroundColor="Transparent"> </ContentPage>

Another one suggest:

<StackLayout VerticalOptions="End" HorizontalOptions="FillAndExpand"  BackgroundColor="#80000000" Opacity="0.5" HeightRequest="160"  Grid.Row="2" >

So both work on tablets but not on mobile devices. Could someone explain to me why and / or suggest me how to solve it? Thanks in advance

Upvotes: 0

Views: 2414

Answers (1)

Lucas Zhang
Lucas Zhang

Reputation: 18861

You could use AbsoluteLayout and set the transparent of the "float" view .

<AbsoluteLayout>

       <!--below view-->
        <StackLayout BackgroundColor="Red" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">

            <Button Text="111111" />

        </StackLayout>


        <!--float view-->
        <StackLayout BackgroundColor="Gray" Opacity="0.5" AbsoluteLayout.LayoutBounds="0.5,0,1,0.5" AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">

            

        </StackLayout>


    </AbsoluteLayout>

Update

It is impossible to implement it if use Navigation . Because it is not enough to set the backgroundColor of Page . There are Rendering layers in the ContentPage .

As a workaround , we could simulate a navigation (open a new page) .

<AbsoluteLayout>

            <!--below view-->
            <StackLayout BackgroundColor="Red" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">

                <Button Text="open new page"  Clicked="Button_Clicked_1"/>

            </StackLayout>


            <!--float view-->
            <StackLayout x:Name="FloatView" BackgroundColor="Gray" Opacity="0.5" AbsoluteLayout.LayoutBounds="1,1,0.01,1" AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">

                <Label  Text="this is a transparent view"  HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" />

                <Button Text="Back" Clicked="Button_Clicked"/>

            </StackLayout>


        </AbsoluteLayout>
private void Button_Clicked_1(object sender, EventArgs e)
        {

            //show
            Device.BeginInvokeOnMainThread(async () =>
            {


                var xPosition = 0;
                var currentPosition = 0.9;
                while (currentPosition >xPosition)
                {
                    await Task.Delay(1);
                    currentPosition -= 0.04;

                    AbsoluteLayout.SetLayoutBounds(FloatView, new Rectangle(currentPosition,0, 1, 1));
                }

            });
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            //hide
            Device.BeginInvokeOnMainThread(async () =>
            {


                AbsoluteLayout.SetLayoutBounds(FloatView, new Rectangle(1, 0, 0.01, 0.01));


            });
        }

enter image description here

Upvotes: 1

Related Questions