Xamarin.Forms 中的透明页面

编程入门 行业动态 更新时间:2024-10-14 20:23:38
本文介绍了Xamarin.Forms 中的透明页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我需要能够为 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/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft/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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-24 17:54:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1068444.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:透明   页面   Xamarin   Forms

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!