[英]How set tab 2 in a xamarin forms Tabbed Page as the default tab on startup
我有一個正在使用 xamarin 形式的應用程序,它看起來像這樣:
當應用程序加載好友選項卡是第一個要加載的選項卡時,如何在應用程序啟動時將其設置為要加載的第一個選項卡?
這是我的 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>
繼承人我的代碼背后:
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace AppName
{
public partial class HomePage : TabbedPage
{
public HomePage()
{
InitializeComponent();
}
}
}
過去 2 天我在谷歌上搜索過,所以我想是時候問了!
您可以訪問TabbedPage
子項的枚舉器並將其位置推進兩次以獲得“第二個選項卡”。 將該頁面指定為您的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];
}
我認為您必須設置“CurrentPage”屬性。
在代碼中是這樣的
TabbedPage tp = new TabbedPage();
tp.Children.Add(new PageFriends());
tp.Children.Add(new PageSnap());
tp.Children.Add(new PageNotes());
tp.CurrentPage = tp.Children[1];
對我來說,我所做的是這樣的:
(this.Parent as TabbedPage).CurrentPage = (this.Parent as TabbedPage).Children[2];
因為在我的標簽頁中,我添加了這樣的頁面
this.Children.Add(new Landing());
this.Children.Add(new Categories());
this.Children.Add(new Collections());
this.Children.Add(new Options());
上面的代碼片段將引導我進入收藏頁面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.