Title in Masterdetail - Xamarin Forms

Asked

Viewed 322 times

2

I need to have title "Main Screen" in a Masterdetail that I have, is 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 even, as in the photo below:

inserir a descrição da imagem aqui

  • Done, if you can give a force thank you.

1 answer

1


By your code, on startup of the app you must be doing something like this:

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

And that’s why you needed to 'hide the NavigationBar'. I think that’s not quite the scenario for which the MasterDetailPage was designed. The CNTP would require an app boot so:

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 the root page

In your case, could you put a NavigationPage fixed as Detail (do not need to hide navigation bar, masterdetail already deals with it) and navigate from it, if so prefer.

Could stay 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 you navigate to another page, it would be 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 in detail page on display.

What you have is the common usage template for this type of page: the container page (The instance of MasterDetailPage); one Menu (which is defined as Master) and the page on display (which will be the Detail). So, just set the title you want on the page that will appear on the Detail and all right.

If the Detail page was declared on XAML it would be something like that:

<?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 the type MasterDetailPage really has some particularities. In the Xamarin.com forum there is a comment from one of the developers of the Xamarin team that clarifies things a little.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.