在 Xamarin Forms Shell 上设置默认选项卡

Nir*_*edi 5 xamarin.forms xamarin.forms.shell

如何在 Xamarin Forms shell 上的选项卡栏中设置默认选定的选项卡?

<?xml version="1.0" encoding="UTF-8"?>
<Shell xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
         x:Class="TestApp.NavigationShell"
         xmlns:pages="clr-namespace:TestApp.Pages"
         xmlns:pageModels="clr-namespace:TestApp.PageModels">
<TabBar Route="testapp">
    <Tab Title="Saved" Icon="tabDashboard" Route="dashboard"><ShellContent ContentTemplate="{DataTemplate pages:DashBoardPage}"/></Tab>
    <Tab Title="Calendar" Icon="tabCalendar" Route="calendar"><ShellContent ContentTemplate="{DataTemplate pages:CalendarPage}"/></Tab>
    <Tab Title="Search" Icon="tabSearch" Route="search"><ShellContent ContentTemplate="{DataTemplate pages:SearchPage}"/></Tab>
    <Tab Title="Support" Icon="tabSupport" Route="support"><ShellContent ContentTemplate="{DataTemplate pages:SupportPage}"/></Tab>
    <Tab Title="Profile" Icon="tabAccount" Route="account"><ShellContent ContentTemplate="{DataTemplate pages:AccountPage}"/></Tab>
</TabBar>
Run Code Online (Sandbox Code Playgroud)

根据 Xamarin Forms 文档,第一个 Shell 内容将是屏幕上的默认内容。然而; 我试图将“搜索”页面设置为默认选项卡,而不是“已保存”。

我尝试设置 Tab Index - 不走运,我还尝试在 Shell 的 onAppearing 方法上调用路由,但似乎 Shell 的 onappearing 方法永远不会被触发。

我也尝试导航到“搜索”:

public partial class NavigationShell : Shell
{
    public NavigationShell()
    {
        InitializeComponent();
        //Shell.Current.TabIndex = 2;
    }

    protected override async void OnAppearing()
    {
        base.OnAppearing();
        //await Shell.Current.GoToAsync("testapp/search");
    }
}
Run Code Online (Sandbox Code Playgroud)

当应用程序打开时需要设置默认选项卡的最佳解决方案是什么?

谢谢。

Nir*_*edi 0

没关系,我想我找到了解决方案。

public Page Init(Shell root)
{
    CurrentShell = root;
    root.CurrentItem.CurrentItem = root.CurrentItem.Items[2];
    return CurrentShell;
}
Run Code Online (Sandbox Code Playgroud)

因为我的 shell 只有一个根项目,即选项卡本身。我刚刚获得搜索选项卡并分配给第一个孩子的当前项目,它就起作用了。

如果您找到其他方法,请发布您的答案。谢谢。