2017-08-12 80 views
0

我希望我的应用程序中的每个页面都有一个顶部的透明工具栏和覆盖整个页面的背景图像。Xamarin.Forms - 通过工具栏使背景图像可见

在App.xaml.cs我写了下面的:

MainPage = new NavigationPage(new MDMaster()) 
{ 
    BarBackgroundColor = Color.Transparent, 
    BackgroundImage = "background_1.png" 
}; 

这将使得正确的工具栏透明,但是背景图像不显示。我可以添加背景颜色而不是背景图片。

MDMaster是MasterDetailPage的主要组件。在MDMaster页面我设置的详细信息页面,如下所示:

Detail = new NavigationPage(new ProfilePage()) 
{ 
    BarBackgroundColor = Color.Transparent, 
    BackgroundImage = "background_1.png" 
}; 

这甚至不显示背景图片无论是。

在ProfilePage,我可以这样写:

public ProfilePage() 
{ 
    InitializeComponent();   
    this.BackgroundImage = "background_1.png"; 
} 

这将显示背景图片,但它不会覆盖,即使它是透明的工具栏后面的区域。它结束了看起来像这样:

Background not showing behind toolbar

+0

1.不建议MD页面添加到导航page.2。 “MDMaster是MasterDetailPage的主要组件”。它应该是MD页面本身,而不是主要组件。 –

+0

那么只有详细页面应该是导航页面,而不是主页面? – Fayze

+0

你是对的 –

回答

2

首先,@Yuri小号建议,你不需要来包装你MasterDetailPageNavigationPage。只需将MasterDetailPage设置为MainPage即可。

然后,当您将背景图像设置为根MasterDetailPage时,背景图像未显示的原因是因为顶部的NavigationPage不透明并且隐藏了背景图像。

因此,要解决这个问题,你需要设置NavigationPage to transparentBackgroundColor太:

public App() 
{ 
    InitializeComponent(); 

    MainPage = new MasterDetailPage() 
    { 
     Master=new MainPage(), 
     Detail=new NavigationPage(new Detail()) 
     { 
      BarBackgroundColor=Color.Transparent, 
      BackgroundColor=Color.Transparent 
     }, 
     BackgroundImage="tianyuan.jpg" 
    }; 
} 

然后,它会显示这样的:

enter image description here

如果你不想让你的状态栏显示如上,你可以在Droid项目中手动设置:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity 
{ 
    protected override void OnCreate(Bundle bundle) 
    { 
     TabLayoutResource = Resource.Layout.Tabbar; 
     ToolbarResource = Resource.Layout.Toolbar; 

     base.OnCreate(bundle); 

     global::Xamarin.Forms.Forms.Init(this, bundle); 
     LoadApplication(new App()); 

     //set the status bar color 
     Window.SetStatusBarColor(Android.Graphics.Color.Black); 
    } 
} 

,它会显示这样的:

enter image description here

+0

啊好吧,这表明背景正确。我有一个问题,但我在MasterPage中有几个Navigation.PushAsync(新的Page())。由于母版页不再是导航页面,它们不再工作了。我该怎么做才能做到这一点?我想每个页面上的后退按钮,但MainPage(MasterDetailPage) – Fayze

+0

'MasterPage'是用于导航的页面。你为什么要改变它?我认为你应该重新考虑你的项目设计。此外,如果您有其他要求,请打开一个新问题,以便每个人都可以通过谷歌找到该问题。 –

+0

我不想改变它,但是如果不是导航页面,我该如何从MasterPage导航? – Fayze