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