[英]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:我有一个正在使用 xamarin 形式的应用程序,它看起来像这样:
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:这是我的 xaml 代码:
<?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!过去 2 天我在谷歌上搜索过,所以我想是时候问了!
You can access the enumerator of the TabbedPage
's children and advance its position two times to get your "second tab".您可以访问
TabbedPage
子项的枚举器并将其位置推进两次以获得“第二个选项卡”。 Assign that page as your CurrentPage
:将该页面指定为您的
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.我认为您必须设置“CurrentPage”属性。
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.上面的代码片段将引导我进入收藏页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.