Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How set tab 2 in a xamarin forms Tabbed Page as the default tab on startup

I have an app I am working on in xamarin forms it looks like this:

enter image description here

When the app loads the friends tab is the 1st tab to load how do I make it to where the snap tab is the 1st tab to load when the app starts up?

Here is my xaml code:

<?xml version="1.0" encoding="UTF-8"?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
            xmlns:local="clr-namespace:AppName;assembly=AppName"
            x:Class="AppName.HomePage">
    <TabbedPage.Children>
        <NavigationPage x:Name="Friends" Title="Friends" Icon="firendstab.png">
            <x:Arguments>
                <local:FriendPage />
            </x:Arguments>
        </NavigationPage >
        <NavigationPage  x:Name="Snap" Title="Snap" Icon="snaptab.png">>
            <x:Arguments>
                <local:CameraPage />
            </x:Arguments>
        </NavigationPage>
        <NavigationPage x:Name="Notes" Title="Notes" Icon="notetab.png">
            <x:Arguments>
                <local:NotePage />
            </x:Arguments>
        </NavigationPage>
    </TabbedPage.Children>
</TabbedPage>

heres my code behind:

using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace AppName
{
    public partial class HomePage : TabbedPage
    {
        public HomePage()
        {
            InitializeComponent();






        }
    }
}

I've searched google for the past 2 days so I thought it was time to ask!

like image 434
Phoneswapshop Avatar asked Mar 17 '17 17:03

Phoneswapshop


2 Answers

You can access the enumerator of the TabbedPage's children and advance its position two times to get your "second tab". Assign that page as your CurrentPage:

public HomePage()
{
    InitializeComponent();
    var pages = Children.GetEnumerator();
    pages.MoveNext(); // First page
    pages.MoveNext(); // Second page
    CurrentPage = pages.Current;
}
like image 154
SushiHangover Avatar answered Oct 09 '22 13:10

SushiHangover


For me, what I did was this:

  (this.Parent as TabbedPage).CurrentPage = (this.Parent as TabbedPage).Children[2];

because in my tabbed page, I added my pages like this

this.Children.Add(new Landing());
this.Children.Add(new Categories());
this.Children.Add(new Collections());
this.Children.Add(new Options());

The above code snippets, will lead me to the Collections Page.

like image 40
Dara Oladapo Avatar answered Oct 09 '22 15:10

Dara Oladapo