Xamarin Forms MasterDetailPage 主页隐藏菜单按钮

Xamarin Forms MasterDetailPage Main Page Hide Menu Button

我有一个针对 Android、IOS 和 UWP 的 Xamarin Forms 项目。 在我的 Xamarin.Forms 项目 App.cs 中,我将 MainPage 设置为继承 class 的 MasterDetailPage。 我已经创建了一个自定义导航栏,所以我不想看到默认的。

我已按照步骤隐藏 IOS 和 Android 的默认栏。 现在我也想为 UWP 隐藏它。

我在 Xamarin Forms 项目中尝试过:

NavigationPage.SetHasNavigationBar(this, false);

在 UWP 项目中(我同时尝试了单独和全部):

Windows.UI.ViewManagement.StatusBar.GetForCurrentView().HideAsync();
Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().SuppressSystemOverlays = true;

但没有任何帮助,所以在 UWP 中我仍然看到 two menubuttons。在图像中我喜欢隐藏灰色按钮(及其所属的栏)。

如果我在 App.cs 中将 MainPage 设置为 NavigationPage 并且不使用 MasterDetailPage,则以下代码有助于删除栏:

NavigationPage.SetHasNavigationBar(this, false);

如何使用 MasterDetailPage 解决这个问题?

感谢您的帮助!

编辑:添加代码。

我在 MainPage 和 MenuPage 中看到了导航栏。 名为 Menubar 的 StackLayout 是我希望看到的唯一顶部栏。

App.cs:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;


namespace AddressReadApp2
{
    public class App : Application
    {

        public App()
        {

                MainPage = new MasterDetail();
            NavigationPage.SetHasNavigationBar(this, false);
            NavigationPage.SetHasNavigationBar(MainPage, false);

        }


        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }

    }
}

MasterDetail.cs:

namespace AddressReadApp2
{
    public class MasterDetail : MasterDetailPage
    {       
        public MasterDetail ()
        {


            NavigationPage.SetHasNavigationBar(this, false);
            Detail = new NavigationPage(new MainPage());
            NavigationPage.SetHasNavigationBar(Detail, false);
            BackgroundColor = Color.FromRgb(0, 170, 167);
            Title = "ibi";

            Master = new MenuPage();           

            MasterBehavior = MasterBehavior.Popover;
        }

    }
}

MenuPage.xaml.cs:

namespace AddressReadApp2.Pages
{
    public partial class MenuPage : ContentPage
    {
        MasterDetail __parent = null;
        MasterDetail _parent
        {
            get
            {
                if (__parent == null)
                    __parent = (MasterDetail)this.Parent;
                return __parent;
            }
        }
        MainPage __detail;
        MainPage _detail
        {
            get
            {
                if (__detail == null && _parent != null)
                    __detail = (MainPage)((NavigationPage)_parent.Detail).CurrentPage;
                return __detail;
            }
        }

        public MenuPage()
        {

            NavigationPage.SetHasNavigationBar(this, false);
            Title = "ibi";
            //Icon = "ibi_logo.png";


            InitializeComponent();

            this.BackgroundColor = Color.FromRgb(0, 170, 167);


            menu.GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(() => _parent.IsPresented = !_parent.IsPresented) });

            Device.OnPlatform(
                iOS: () =>
                {
                    MenuBar.Padding = new Thickness(0, 25, 0, 0);
                }
                );
        }
}

MenuPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="AddressReadApp2.Pages.MenuPage">
  <StackLayout Orientation="Vertical" VerticalOptions="FillAndExpand">
    <StackLayout x:Name="MenuBar" Orientation="Horizontal">
      <Image x:Name="menu" Source="menu.png" HeightRequest="35" WidthRequest="35" HorizontalOptions="Start" Margin="10,2,0,0" />
      <Image x:Name="logo" Source="ibi_logo2.png" HeightRequest="35" WidthRequest="35" HorizontalOptions="EndAndExpand" Margin="0,0,10,2" />
    </StackLayout>
    <Grid RowSpacing="2" ColumnSpacing="0">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition Height="50" />
      </Grid.RowDefinitions>
      <!-- Between here are Labels for the menu -->
    </Grid>
  </StackLayout>
</ContentPage>

MainPage.xaml.xs:

namespace AddressReadApp2
{
    public partial class MainPage : ContentPage
    {

        public MainPage()
        {

            InitializeComponent();

            NavigationPage.SetHasNavigationBar(this, false);
            MenuBar.BackgroundColor = Color.White; 

            menu.GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(() => ((MasterDetailPage)Parent.Parent).IsPresented = true) });
        }
    }
}

MainPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"             
             x:Class="AddressReadApp2.MainPage" BackgroundColor="White" NavigationPage.TitleIcon="icon.png" x:Name="MainPage">

  <StackLayout x:Name="mainStack" Orientation="Vertical" VerticalOptions="FillAndExpand">
    <StackLayout x:Name="MenuBar" Orientation="Horizontal">
      <Image x:Name="menu" Source="menu2.png" HeightRequest="35" WidthRequest="35" HorizontalOptions="Start" Margin="10,2,0,0" />
      <Image x:Name="logo" Source="ibi_logo.png" HeightRequest="35" WidthRequest="35" HorizontalOptions="EndAndExpand" Margin="0,0,10,2" />
    </StackLayout>
    <StackLayout x:Name="pnlStart" Orientation="Vertical" VerticalOptions="Fill" HorizontalOptions="Fill">
      <StackLayout x:Name="pnlWelcome" VerticalOptions="End" Padding="10">
        <Label Text="Welcome" VerticalOptions="Center" HorizontalOptions="Center" FontSize="25" />        
      </StackLayout>
    </StackLayout>
  </StackLayout>
</ContentPage>

我想这样就可以了

public class CustomMasterDetailRenderer :MasterDetailPageRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<MasterDetailPage> e)
    {
        base.OnElementChanged(e);
        if (Control != null)
        {   
            Control.CollapsedPaneWidth = 0;
            Control.CollapseStyle = Xamarin.Forms.PlatformConfiguration.WindowsSpecific.CollapseStyle.Partial;
            Control.MasterToolbarVisibility = Windows.UI.Xaml.Visibility.Collapsed;
            Control.DetailTitleVisibility = Windows.UI.Xaml.Visibility.Collapsed;
            Control.MasterTitleVisibility = Windows.UI.Xaml.Visibility.Collapsed;
            Control.ContentTogglePaneButtonVisibility = 
 Windows.UI.Xaml.Visibility.Collapsed;
        }
    }
}

现在你可以这样调用了我认为你的调用和代码是正确的但是你需要为 UWP 自定义渲染

NavigationPage.SetHasBackButton(this, false);
NavigationPage.SetHasNavigationBar(this, false);