Transparent page in Xamarin.Forms

允我心安 提交于 2021-02-10 16:20:35

问题


I need to be able to create a transparent Xamarin.Forms page for Android. How can I do this true a page renderer? Now it has a default background color.

[assembly: ExportRenderer(typeof(MyPage), typeof(ClearBackgroundPageRenderer))]
namespace MyApp.Droid
{
    public class ClearBackgroundPageRenderer : PageRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
        {
            base.OnElementChanged(e);

            SetBackgroundColor(Color.Transparent.ToAndroid());
        }
    }
}

回答1:


If you just want to make your page's background transparent, you don't need to create a custom renderer for this. You can set the background color in PCL.

For example, xaml:

<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>

Or in code behind:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        this.BackgroundColor = Color.Transparent;
    }
}

To prove it's transparent, we can use a NavigationPage with colored background for testing in App.xaml.cs:

public App()
{
    InitializeComponent();

    MainPage = new NavigationPage(new MainPage())
    {
        BackgroundColor = Color.Red
    };
}


来源:https://stackoverflow.com/questions/43862214/transparent-page-in-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!