Xamarin表单MasterDetailPage主页隐藏菜单按钮

时间:2016-12-19 14:04:05

标签: c# xamarin xamarin.forms navigationbar xamarin.uwp

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

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

我在Xamarin Forms Project中尝试过:

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中看到了导航栏。 名为菜单栏的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>

1 个答案:

答案 0 :(得分:2)

我认为这样做

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);