简体   繁体   中英

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:

在此处输入图片说明

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!

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;
}
public HomePage()
{
    InitializeComponent();
    CurrentPage = Children[2];
}

I think you have to set "CurrentPage" property.

In code is something like

            TabbedPage tp = new TabbedPage();
            tp.Children.Add(new PageFriends());
            tp.Children.Add(new PageSnap());
            tp.Children.Add(new PageNotes());
            tp.CurrentPage = tp.Children[1];

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM