Xamarin.Forms 中的透明页面 [英] Transparent page in Xamarin.Forms

查看:46
本文介绍了Xamarin.Forms 中的透明页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够为 Android 创建透明的 Xamarin.Forms 页面.我怎样才能做到这一点真正的页面渲染器?现在它有一个默认的背景颜色.

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());
        }
    }
}

推荐答案

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

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.

例如,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>

或者在代码后面:

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

为了证明它是透明的,我们可以在App.xaml.cs中使用一个带有彩色背景的NavigationPage进行测试:

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

这篇关于Xamarin.Forms 中的透明页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆