【问题标题】:Set default tab on Xamarin Forms Shell在 Xamarin Forms Shell 上设置默认选项卡
【发布时间】:2019-11-10 17:28:13
【问题描述】:

如何在 Xamarin 表单 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 内容将是屏幕上的默认内容。然而;我正在尝试将“搜索”页面设置为默认选项卡,而不是“已保存”。

我尝试设置标签索引 - 没有运气 我也尝试在 Shell 的 onAppearing 方法上调用路由,但似乎 Shell 的 on Appearing 方法永远不会被触发。

我也尝试导航到搜索:

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

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

谢谢。

【问题讨论】:

    标签: xamarin.forms xamarin.forms.shell


    【解决方案1】:

    没关系,我想我找到了解决办法。

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

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

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

    【讨论】:

    【解决方案2】:

    Shell 类的构造函数中,您可以设置CurrentItem

    在我的情况下,我有一个 &lt;ShellItem&gt; 和一些 &lt;tab&gt;

    public PrivateShell()
    {
        InitializeComponent();
        this.CurrentItem.CurrentItem = homeTab; //tab defined by x:Name on XAML
    }
    

    据我所知,如果您使用 ContentTemplate 模式,它不会预渲染第一个 shell 内容

    【讨论】:

    • 我正在使用 ContentTemplate 模式
    • 那么当使用它时它不会实例化第一个shellitem,只会实例化你选择的shellitem
    【解决方案3】:

    这里,我如何通过使用 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 2014-06-25
      • 2019-11-17
      相关资源
      最近更新 更多