Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a default page in the Xamarin.Forms shell menu

Using the Xaminals example, I'm trying to set the default startup page. Here's the relevant code from the xaml:

<FlyoutItem Route="animals" x:Name="shellAnimals"
            Title="Animals"
            FlyoutDisplayOptions="AsMultipleItems">
    <Tab Title="Domestic" x:Name="shellDomestic"
         Route="domestic"
         Icon="paw.png">
        <ShellContent Route="cats" x:Name="shellCats"
                      Style="{StaticResource DomesticShell}"
                      Title="Cats"
                      Icon="cat.png"
                      ContentTemplate="{DataTemplate views:CatsPage}" />
        <ShellContent Route="dogs" x:Name="shellDogs"
                      Style="{StaticResource DomesticShell}"
                      Title="Dogs"
                      Icon="dog.png"
                      ContentTemplate="{DataTemplate views:DogsPage}" />
    </Tab>
    <ShellContent Route="monkeys" x:Name="shellMonkeys"
                  Style="{StaticResource MonkeysShell}"
                  Title="Monkeys"
                  Icon="monkey.png"
                  ContentTemplate="{DataTemplate views:MonkeysPage}" />
    <ShellContent Route="elephants" x:Name="shellElephants"
                  Style="{StaticResource ElephantsShell}"
                  Title="Elephants"
                  Icon="elephant.png"
                  ContentTemplate="{DataTemplate views:ElephantsPage}" />
    <ShellContent Route="bears" x:Name="shellBears"
                  Style="{StaticResource BearsShell}"
                  Title="Bears"
                  Icon="bear.png"
                  ContentTemplate="{DataTemplate views:BearsPage}" />
</FlyoutItem>

In the code behind I can successfully set the default startup page.

For example, the dogs page:

shellAnimals.CurrentItem.CurrentItem = shellDogs;

But on the bears page:

// This works, but I don't like the indexing. Will give trouble on dynamic menu's
shellAnimals.CurrentItem = shellAnimals.Items[3];

// This doesn't work, but I don't understand why. It gives a null reference exception in the Xamarin Shell code, on the PushAsync
shellAnimals.CurrentItem = shellBears;

// This shows the bears page at startup, but when I click on an item in the listview, I also get a null reference error
CurrentItem = shellBears;
  1. Why doesn't the second/third snippet work?
  2. How can I set the default page to the bears page without using the indexer?

Can't find anything on this subject except Set default tab on Xamarin Forms Shell but that doesn't answer it.

like image 983
Jasper Avatar asked Dec 09 '19 12:12

Jasper


People also ask

How do I navigate to shell page in xamarin forms?

Navigating backwards and into a specified route is only possible if the backwards navigation places you at the current location in the route hierarchy to navigate to the specified route. await Shell. Current. GoToAsync("../../route");

What is flyout page in xamarin forms?

A flyout page typically displays a list of items, as shown in the following screenshots: The location of the list of items is identical on each platform, and selecting one of the items will navigate to the corresponding detail page.

What is Shell in xamarin forms?

Xamarin. Forms Shell reduces the complexity of mobile application development by providing the fundamental features that most mobile applications require, including: A single place to describe the visual hierarchy of an application. A common navigation user experience.

What is Shell page?

A page shell is an content page, basically serving as a placeholder for content at a later point.


2 Answers

You can set the current page in XAML, as per the documentation Set-the-current-flyoutitem. This would then mean your Shell tag, you would add `CurrentItem="{x:Reference shellBears}"1.

Additionally, I see in your second code behind snippet you are missing another .CurrentItem step when compared to the dogs example that you say works. Can you try shellAnimals.CurrentItem.CurrentItem = shellBears;

like image 136
Lindsay Avatar answered Oct 23 '22 05:10

Lindsay


Why doesn't the second/third snippet work?

The second snippet:

shellAnimals.CurrentItem is a variable which type is ShellSection while your shellBears is kind of ShellContent.

The third snippet:

When you use CurrentItem directly in the shell class, that is Shell.CurrentItem and it's a ShellItem.

How can I set the default page to the bears page without using the indexer?

In your Xaml:

    <ShellSection x:Name="shellBears" Title="Bears"
                      Icon="bear.png">
        <ShellContent Route="bears"
                      Style="{StaticResource BearsShell}"                                                   
                      ContentTemplate="{DataTemplate views:BearsPage}" />
    </ShellSection >

And in code behind:

    shellAnimals.CurrentItem = shellBears;
like image 24
nevermore Avatar answered Oct 23 '22 04:10

nevermore