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

5
如何在 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>

根据Xamarin Forms文档,第一个Shell Content将成为屏幕上的默认内容。然而,我想将“搜索”页面设置为默认选项卡,而不是“已保存”的选项卡。
我尝试设置标签索引 - 无效。我还尝试在Shell的onAppearing方法中调用路由,但似乎Shell的onAppearing方法从未触发。
我也试图导航到Search页面:
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");
    }
}

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

谢谢。

3个回答

2

使用Name属性,我是如何解决这个问题的。

我给TabBar和我想要首先分配的选项卡分配了名称。

AppShell.xaml

<TabBar x:Name="main_tab_bar" Route="main">

    <Tab Title="History">
        <Tab.Icon>
            <FontImageSource FontFamily="MaterialIconsRegular" Glyph="{x:Static Emojis:MaterialRegFont.List}"/>
        </Tab.Icon>
        <ShellContent ContentTemplate="{DataTemplate Views:CallLogPage}" />
    </Tab>

    <Tab x:Name="main_tab_bar_dial"  Title="Dial" >
        <Tab.Icon>
            <FontImageSource FontFamily="MaterialIconsRegular" Glyph="{x:Static Emojis:MaterialRegFont.Dialpad}"/>
        </Tab.Icon>
        <ShellContent ContentTemplate="{DataTemplate Views:MainDialerPage}" />
    </Tab>

    <Tab Title="Settings">
        <Tab.Icon>
            <FontImageSource FontFamily="MaterialIconsRegular" Glyph="{x:Static Emojis:MaterialRegFont.Settings}"/>
        </Tab.Icon>
        <ShellContent ContentTemplate="{DataTemplate Views:MainSettingsPage}" />
    </Tab>

</TabBar>

AppShell.cs

public AppShell() {
    InitializeComponent(); 
    Init();
}

private void Init() {
    main_tab_bar.CurrentItem = main_tab_bar_dial;
}

0

算了,我想我找到了一个解决方案。

public Page Init(Shell root)
{
    CurrentShell = root;
    root.CurrentItem.CurrentItem = root.CurrentItem.Items[2];
    return CurrentShell;
}

因为我的 shell 只有一个根项,即选项卡本身。我只需获取搜索选项卡并将其分配给第一个子项的当前项,就可以解决问题。

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


这是我写的一篇关于在Shell中设置默认项目的文章:https://medium.com/@jasper76/selecting-a-default-flyout-menu-item-in-xamarin-forms-shell-626d9abe9b58 - Jasper

0
在你的Shell类的构造函数中,你可以设置CurrentItem
在我的情况下,我有一个带有一些<tab><ShellItem>
public PrivateShell()
{
    InitializeComponent();
    this.CurrentItem.CurrentItem = homeTab; //tab defined by x:Name on XAML
}

据我所知,如果您使用ContentTemplate模式,它似乎不会预渲染第一个shellcontent。


我正在使用 ContentTemplate 模式。 - N Subedi
然后在使用它时,它不会实例化第一个shellitem,而只会实例化您选择的shellitem。 - Patrick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接