2017-05-09 107 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