Title in MasterDetail - Xamarin Forms

2

I need to have the title "Main Screen" in a MasterDetail that I have, is it possible?

<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              x:Class="FoodSuppy.Principal"
              xmlns:local="clr-namespace:FoodSuppy;assembly=FoodSuppy"
              NavigationPage.HasNavigationBar="False">
<!-- Sair -->
<MasterDetailPage.ToolbarItems>
    <ToolbarItem Text="Sair"
                 Clicked="Sair_Clicked"/>
</MasterDetailPage.ToolbarItems> 

<!-- Menu -->
<MasterDetailPage.Master>
    <ContentPage Title="Menu" 
                 Icon="menu.png">
        <StackLayout>
            <Button Text="Pag Principal" 
                    Clicked="btnPagina1_Clicked"/>
            <Button Text="Alterar cadastro" 
                    Clicked="btnPagina2_Clicked"/>
            <Button Text="Ajuda" 
                    Clicked="btnAjuda_Clicked"/>
            <Button Text="Sobre"/>
        </StackLayout>
    </ContentPage>
</MasterDetailPage.Master>

Note: I'm hiding the Navigation bar, only the MasterDetail bar appears, as in the picture below:

    
asked by anonymous 27.04.2018 / 14:48

1 answer

1

For your code, when you start the app you should be doing something like this:

App.Current.MainPage = new NavigationPage(new FoodSuppy.Principal());

And that's why you had to 'hide the NavigationBar '. I think this is not quite the scenario for which MasterDetailPage was projected. The CNTP would require an initialization of the app like this:

App.Current.MainPage = new FoodSuppy.Principal();

According to the documentation ,

  

Developers should only use master detail pages as root page.

That is (in a free translation):

  

MasterDetailPage should only be used as root page

In your case, you could put a NavigationPage fixed as Detail (you would not need to hide the navigation bar, the masterdetail already handles this) and would browse from it if you prefer.

It could look like this:

<MasterDetailPage.Master>
    <!-- Sua master -->
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
    <NavigationPage>
        <x:Arguments>
            <!-- a página inicial do 'Detail' viria aqui --/>
        </x:Arguments>
    </NavigationPage>
</MasterDetailPage.Detail>

And when I navigate to another page, it would look something like this (C #):

var principal =  App.Current.MainPage as MasterDetailPage;
var emExibicao = (principal?.Detail as NavigationPage);
emExibicao?.PushAsync(new OutraPaginaQualquer());

The title that will be displayed in the main will always be the title defined on the detail page that is currently displayed.

What you have is the common use template for this page type: the container page (The instance of MasterDetailPage ); a Menu (which is set to Master ) and the running page (which will be Detail ). So, just set the title you want on the page that will appear in the detail and everything is fine.

If the detail page was declared in XAML it would look something like this:

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

    <!-- conteúdo anterior da sua página -->

    <MasterDetailPage.Detail>
        <ContentPage Title="Tela Principal">
            <Label Text="Pag Principal"/>
            <!-- Conteúdo da página-->
        </ContentPage>
    </MasterDetailPage.Detail>
</MasterDetailPage>

The page of type MasterDetailPage actually has some peculiarities. In the Xamarin.com forum you have a comment from one of the developers of the Xamarin team that clarifies things a little.

    
27.04.2018 / 19:04