2017-05-09 296 views
-2

我需要能够为Android创建一个透明的Xamarin.Forms页面。我怎样才能做到这一点真正的页面渲染器?现在它有一个默认的背景颜色。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()); 
     } 
    } 
} 
+1

欢迎来到[Stack Overflow](http://stackoverflow.com/)!请阅读[如何提问](http://stackoverflow.com/help/how-to-ask)并提供[最小,完整和可验证示例](http://stackoverflow.com/help/mcve) ! –

回答

1

如果您只是想使页面的背景透明,则不需要为此创建自定义渲染器。您可以在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与测试彩色背景:

public App() 
{ 
    InitializeComponent(); 

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

这不行? – Daniel