C# Xamarin.Forms中的透明页面

C# Xamarin.Forms中的透明页面,c#,android,xamarin,xamarin.android,xamarin.forms,C#,Android,Xamarin,Xamarin.android,Xamarin.forms,我需要能够为Android创建一个透明的Xamarin.Forms页面。如何在页面渲染器中实现这一点?现在它有了默认的背景色 [assembly: ExportRenderer(typeof(MyPage), typeof(ClearBackgroundPageRenderer))] namespace MyApp.Droid { public class ClearBackgroundPageRenderer : PageRenderer { protected

我需要能够为Android创建一个透明的Xamarin.Forms页面。如何在页面渲染器中实现这一点?现在它有了默认的背景色

[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());
        }
    }
}
[程序集:ExportRenderer(typeof(MyPage)、typeof(ClearBackgroundPageRenderer))]
名称空间MyApp.Droid
{
公共类ClearBackgroundPageRenderer:PageRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
SetBackgroundColor(Color.Transparent.ToAndroid());
}
}
}

如果您只想使页面背景透明,则无需为此创建自定义渲染器。您可以在PCL中设置背景色

例如,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>
为了证明它是透明的,我们可以在
App.xaml.cs
中使用带有彩色背景的
导航页面进行测试:

public App()
{
    InitializeComponent();

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

如果您只想使页面的背景透明,则不需要为此创建自定义渲染器。您可以在PCL中设置背景色

例如,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>
为了证明它是透明的,我们可以在
App.xaml.cs
中使用带有彩色背景的
导航页面进行测试:

public App()
{
    InitializeComponent();

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

欢迎光临!请阅读并提供一个完整的答案!欢迎光临!请阅读并提供一个完整的答案!不起作用。你不能做背景色tranparent@BenBtg,问题是使contentpage控件透明,而不是使整个应用程序透明,因此在我的回答中,contentpage由
NavigationPage
包装,因此当导航到具有透明背景的内容页时,它将显示NavigationPage的背景。为什么说它不起作用?是否有禁用此功能的更新版本?不起作用。你不能做背景色tranparent@BenBtg,问题是使contentpage控件透明,而不是使整个应用程序透明,因此在我的回答中,contentpage由
NavigationPage
包装,因此当导航到具有透明背景的内容页时,它将显示NavigationPage的背景。为什么说它不起作用?是否有禁用此功能的更新版本?