Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms MasterDetailPage Main Page Hide Menu Button

I have a Xamarin Forms project, which targets Android, IOS and UWP. In my Xamarin.Forms Project App.cs I have set the MainPage to a MasterDetailPage inherited class. I have created a custom navigationbar, so I don't want to see the default.

I have followed the steps to hide the default bar for IOS and Android from this question. Now I want to hide it for UWP as well.

I have tried in the Xamarin Forms Project:

NavigationPage.SetHasNavigationBar(this, false);

In the UWP Project (I tried separate and all at the same time):

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

But nothing helps, so in UWP I am still seeing two menubuttons. In the image I like to hide the grey button (and the bar it belongs to).

If I set the MainPage in App.cs to a NavigationPage and not use a MasterDetailPage, the following code helps to remove the bar:

NavigationPage.SetHasNavigationBar(this, false);

How can I solve this with a MasterDetailPage?

Thanks for your help!

Edit: added code.

I see the navigationbar in the MainPage and in the MenuPage. The StackLayout called Menubar is the only bar at the top I would like to see.

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>
like image 931
Sander Avatar asked Dec 19 '16 14:12

Sander


1 Answers

I think this will do

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

now you can call like this I think your calling and code are correct but you need custom rendering for the UWP

NavigationPage.SetHasBackButton(this, false);
NavigationPage.SetHasNavigationBar(this, false);
like image 138
ish1104 Avatar answered Nov 02 '22 22:11

ish1104