0

To begin, I'm a beginner at C# and Xamarin. I have an app with Xamarin Forms and am using the Xamarin Community Toolkit "TabView" to create a Snapchat-style application. I'm trying to make the app open on the second tab out of the three I have in XAML so it will be on the "middle" tab, but it keeps opening the application on the first (left-most) tab. Is there any sort of property to set the order of which opens initially?

I attempted to assign a value to each tab, but my efforts simply do not work. I also tried the TabIndex property, but I don't believe that correlates to the Community Toolkit.

    <!--Overall Tabs-->
<tk:TabView TabStripPlacement="Bottom" IsSwipeEnabled="False" TabStripBackgroundColor="#DD000000">
    <!--Left Tab-->
    <tk:TabViewItem Text="Data"> ... </tk:TabViewItem>
    <!--Middle Tab-->
    <tk:TabViewItem Text="Home"> ... </tk:TabViewItem>
    <!--Right Tab-->
    <tk:TabViewItem Text="Capture"> ... </tk:TabViewItem>
</tk:TabView>

Thanks all

Riley
  • 3
  • 2
  • have you tried `SelectedIndex`? – Jason Mar 27 '22 at 19:38
  • @Jason When putting `SelectedIndex = "1"` in the TabView properties, it says that the index is out of range (must be non-negative and less than the size of the collection). I added code to the initial post so there's a better understanding. – Riley Mar 27 '22 at 19:55
  • you need to set the index in code, **after** the tabs have been initialized – Jason Mar 27 '22 at 20:05
  • @Jason If you don't mind explaining, how would you go about doing that either within the C# code or the XAML? – Riley Mar 27 '22 at 20:47

1 Answers1

0

you need to set it in the code, after the tabs have loaded

first, give your tabs a name

<tk:TabView x:Name="myTabs" TabStripPlacement="Bottom" IsSwipeEnabled="False" TabStripBackgroundColor="#DD000000">

then in the code behind of the page

protected override void OnAppearing()
{
    base.OnAppearing();
    
    myTabs.SelectedIndex = 2;
}    
Jason
  • 86,222
  • 15
  • 131
  • 146