Xamarin表单-在MasterDetailPage中隐藏导航栏

时间:2018-07-02 13:25:55

标签: xamarin xamarin.forms xamarin.ios xamarin.android

我需要在xamarin 3.0表单中隐藏导航栏

我尝试这样做,但是不起作用: Hide Navigation Bar on MasterDetailPage

我要隐藏导航栏以创建自定义栏,我还需要一种打开菜单的方式。 谢谢。

App.xaml.cs

public partial class App : Application
{
    public App ()
    {
        InitializeComponent();

        MainPage = new MainPage();
    }
...

MainPage.xaml.cs

public partial class MainPage : MasterDetailPage
{
    public List<MasterPageItem> menuList { get; set; }
    public MainPage()
    {
        InitializeComponent();

        menuList = new List<MasterPageItem>();

        menuList.Add(new MasterPageItem() { Title = "Home", Icon = "home.png", TargetType = typeof(HomePage) });
        menuList.Add(new MasterPageItem() { Title = "Settings", Icon = "setting.png", TargetType = typeof(SettingsPage) });
        menuList.Add(new MasterPageItem() { Title = "Help", Icon = "help.png", TargetType = typeof(HelpPage) });

        navigationDrawerList.ItemsSource = menuList;

        var navPage = new NavigationPage((Page)Activator.CreateInstance(typeof(HomePage)));

        //I try this:
        NavigationPage.SetHasNavigationBar(navPage, false);
        NavigationPage.SetHasBackButton(navPage, false);

        Detail = navPage;
    }
...

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:App3"
             x:Class="App3.MainPage">

    <MasterDetailPage.Master>
        <ContentPage Title="Menu">
            <Grid BackgroundColor="Transparent">
                <StackLayout Grid.Row="1" Spacing="15">
                   ...
                </StackLayout>
            </Grid>
        </ContentPage>
    </MasterDetailPage.Master>

    <MasterDetailPage.Detail>
        <NavigationPage>

        </NavigationPage>
    </MasterDetailPage.Detail>
</MasterDetailPage>

源回购: https://github.com/uiahhh/stackoverflow/tree/master/HideNavBar

我要在红色圆圈中删除此导航栏: enter image description here

2 个答案:

答案 0 :(得分:3)

详细信息 页面上,您必须在NavigationPage.SetHasNavigationBar(this, false);

之后立即删除构造函数中带有InitializeComponent()的导航栏。
public partial class MyPage : NavigationPage
{
    public MyPage()
    {
        InitializeComponent();
        NavigationPage.SetHasNavigationBar(this, false);
    }
}

答案 1 :(得分:0)

除了 Pedro's answer,您还可以隐藏 .xaml 文件中的导航栏。例如:

<ContentPage NavigationPage.HasNavigationBar="False">
    ...
</ContentPage>

注意:这在 Xamarin 4.0 中有效,但我没有在 3.0 中测试过。