在Xamarin Forms Shell上设置默认选项卡 [英] Set default tab on Xamarin Forms Shell

查看:152
本文介绍了在Xamarin Forms Shell上设置默认选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Xamarin表单外壳的标签栏中设置默认的选定标签?

How to set the default selected Tab inside the tab bar on 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内容将是屏幕上的默认内容.然而;我正在尝试将搜索"页面设置为默认选项卡,而不是已保存"页面.

According to the Xamarin Forms documentation, the first Shell Content will be the default content on the screen. However; I'm trying to set the "Search" page as the default tab rather than "Saved".

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

I tried to set Tab Index - no luck I also tried to call routing on onAppearing method of Shell but seems like Shell's on appearing method never gets fired.

我也尝试导航到搜索

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");
    }
}

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

What could be the best solution that when app opens need to set the default tab?

谢谢.

推荐答案

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

Nevermind, I think I found a solution.

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

如果我的外壳只有一个根项目,即标签栏本身.我只是得到了搜索选项卡,并分配给了第一个孩子的当前项目,所以它起作用了.

Becase my shell have ONLY one root item which is tabbar itself. I just get the search tab and assigned to the first child's current item and it worked.

如果您发现其他方法,请发表您的答案. 谢谢.

Please post your answers if you found another way around. Thank you.

这篇关于在Xamarin Forms Shell上设置默认选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆